via-agp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * VIA AGPGART routines.
  3. */
  4. #include <linux/types.h>
  5. #include <linux/module.h>
  6. #include <linux/pci.h>
  7. #include <linux/init.h>
  8. #include <linux/agp_backend.h>
  9. #include "agp.h"
  10. static const struct pci_device_id agp_via_pci_table[];
  11. #define VIA_GARTCTRL 0x80
  12. #define VIA_APSIZE 0x84
  13. #define VIA_ATTBASE 0x88
  14. #define VIA_AGP3_GARTCTRL 0x90
  15. #define VIA_AGP3_APSIZE 0x94
  16. #define VIA_AGP3_ATTBASE 0x98
  17. #define VIA_AGPSEL 0xfd
  18. static int via_fetch_size(void)
  19. {
  20. int i;
  21. u8 temp;
  22. struct aper_size_info_8 *values;
  23. values = A_SIZE_8(agp_bridge->driver->aperture_sizes);
  24. pci_read_config_byte(agp_bridge->dev, VIA_APSIZE, &temp);
  25. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  26. if (temp == values[i].size_value) {
  27. agp_bridge->previous_size =
  28. agp_bridge->current_size = (void *) (values + i);
  29. agp_bridge->aperture_size_idx = i;
  30. return values[i].size;
  31. }
  32. }
  33. printk(KERN_ERR PFX "Unknown aperture size from AGP bridge (0x%x)\n", temp);
  34. return 0;
  35. }
  36. static int via_configure(void)
  37. {
  38. u32 temp;
  39. struct aper_size_info_8 *current_size;
  40. current_size = A_SIZE_8(agp_bridge->current_size);
  41. /* aperture size */
  42. pci_write_config_byte(agp_bridge->dev, VIA_APSIZE,
  43. current_size->size_value);
  44. /* address to map too */
  45. pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
  46. agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  47. /* GART control register */
  48. pci_write_config_dword(agp_bridge->dev, VIA_GARTCTRL, 0x0000000f);
  49. /* attbase - aperture GATT base */
  50. pci_write_config_dword(agp_bridge->dev, VIA_ATTBASE,
  51. (agp_bridge->gatt_bus_addr & 0xfffff000) | 3);
  52. return 0;
  53. }
  54. static void via_cleanup(void)
  55. {
  56. struct aper_size_info_8 *previous_size;
  57. previous_size = A_SIZE_8(agp_bridge->previous_size);
  58. pci_write_config_byte(agp_bridge->dev, VIA_APSIZE,
  59. previous_size->size_value);
  60. /* Do not disable by writing 0 to VIA_ATTBASE, it screws things up
  61. * during reinitialization.
  62. */
  63. }
  64. static void via_tlbflush(struct agp_memory *mem)
  65. {
  66. u32 temp;
  67. pci_read_config_dword(agp_bridge->dev, VIA_GARTCTRL, &temp);
  68. temp |= (1<<7);
  69. pci_write_config_dword(agp_bridge->dev, VIA_GARTCTRL, temp);
  70. temp &= ~(1<<7);
  71. pci_write_config_dword(agp_bridge->dev, VIA_GARTCTRL, temp);
  72. }
  73. static const struct aper_size_info_8 via_generic_sizes[9] =
  74. {
  75. {256, 65536, 6, 0},
  76. {128, 32768, 5, 128},
  77. {64, 16384, 4, 192},
  78. {32, 8192, 3, 224},
  79. {16, 4096, 2, 240},
  80. {8, 2048, 1, 248},
  81. {4, 1024, 0, 252},
  82. {2, 512, 0, 254},
  83. {1, 256, 0, 255}
  84. };
  85. static int via_fetch_size_agp3(void)
  86. {
  87. int i;
  88. u16 temp;
  89. struct aper_size_info_16 *values;
  90. values = A_SIZE_16(agp_bridge->driver->aperture_sizes);
  91. pci_read_config_word(agp_bridge->dev, VIA_AGP3_APSIZE, &temp);
  92. temp &= 0xfff;
  93. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  94. if (temp == values[i].size_value) {
  95. agp_bridge->previous_size =
  96. agp_bridge->current_size = (void *) (values + i);
  97. agp_bridge->aperture_size_idx = i;
  98. return values[i].size;
  99. }
  100. }
  101. return 0;
  102. }
  103. static int via_configure_agp3(void)
  104. {
  105. u32 temp;
  106. struct aper_size_info_16 *current_size;
  107. current_size = A_SIZE_16(agp_bridge->current_size);
  108. /* address to map too */
  109. pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
  110. agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  111. /* attbase - aperture GATT base */
  112. pci_write_config_dword(agp_bridge->dev, VIA_AGP3_ATTBASE,
  113. agp_bridge->gatt_bus_addr & 0xfffff000);
  114. /* 1. Enable GTLB in RX90<7>, all AGP aperture access needs to fetch
  115. * translation table first.
  116. * 2. Enable AGP aperture in RX91<0>. This bit controls the enabling of the
  117. * graphics AGP aperture for the AGP3.0 port.
  118. */
  119. pci_read_config_dword(agp_bridge->dev, VIA_AGP3_GARTCTRL, &temp);
  120. pci_write_config_dword(agp_bridge->dev, VIA_AGP3_GARTCTRL, temp | (3<<7));
  121. return 0;
  122. }
  123. static void via_cleanup_agp3(void)
  124. {
  125. struct aper_size_info_16 *previous_size;
  126. previous_size = A_SIZE_16(agp_bridge->previous_size);
  127. pci_write_config_byte(agp_bridge->dev, VIA_APSIZE, previous_size->size_value);
  128. }
  129. static void via_tlbflush_agp3(struct agp_memory *mem)
  130. {
  131. u32 temp;
  132. pci_read_config_dword(agp_bridge->dev, VIA_AGP3_GARTCTRL, &temp);
  133. pci_write_config_dword(agp_bridge->dev, VIA_AGP3_GARTCTRL, temp & ~(1<<7));
  134. pci_write_config_dword(agp_bridge->dev, VIA_AGP3_GARTCTRL, temp);
  135. }
  136. static const struct agp_bridge_driver via_agp3_driver = {
  137. .owner = THIS_MODULE,
  138. .aperture_sizes = agp3_generic_sizes,
  139. .size_type = U8_APER_SIZE,
  140. .num_aperture_sizes = 10,
  141. .needs_scratch_page = true,
  142. .configure = via_configure_agp3,
  143. .fetch_size = via_fetch_size_agp3,
  144. .cleanup = via_cleanup_agp3,
  145. .tlb_flush = via_tlbflush_agp3,
  146. .mask_memory = agp_generic_mask_memory,
  147. .masks = NULL,
  148. .agp_enable = agp_generic_enable,
  149. .cache_flush = global_cache_flush,
  150. .create_gatt_table = agp_generic_create_gatt_table,
  151. .free_gatt_table = agp_generic_free_gatt_table,
  152. .insert_memory = agp_generic_insert_memory,
  153. .remove_memory = agp_generic_remove_memory,
  154. .alloc_by_type = agp_generic_alloc_by_type,
  155. .free_by_type = agp_generic_free_by_type,
  156. .agp_alloc_page = agp_generic_alloc_page,
  157. .agp_alloc_pages = agp_generic_alloc_pages,
  158. .agp_destroy_page = agp_generic_destroy_page,
  159. .agp_destroy_pages = agp_generic_destroy_pages,
  160. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  161. };
  162. static const struct agp_bridge_driver via_driver = {
  163. .owner = THIS_MODULE,
  164. .aperture_sizes = via_generic_sizes,
  165. .size_type = U8_APER_SIZE,
  166. .num_aperture_sizes = 9,
  167. .needs_scratch_page = true,
  168. .configure = via_configure,
  169. .fetch_size = via_fetch_size,
  170. .cleanup = via_cleanup,
  171. .tlb_flush = via_tlbflush,
  172. .mask_memory = agp_generic_mask_memory,
  173. .masks = NULL,
  174. .agp_enable = agp_generic_enable,
  175. .cache_flush = global_cache_flush,
  176. .create_gatt_table = agp_generic_create_gatt_table,
  177. .free_gatt_table = agp_generic_free_gatt_table,
  178. .insert_memory = agp_generic_insert_memory,
  179. .remove_memory = agp_generic_remove_memory,
  180. .alloc_by_type = agp_generic_alloc_by_type,
  181. .free_by_type = agp_generic_free_by_type,
  182. .agp_alloc_page = agp_generic_alloc_page,
  183. .agp_alloc_pages = agp_generic_alloc_pages,
  184. .agp_destroy_page = agp_generic_destroy_page,
  185. .agp_destroy_pages = agp_generic_destroy_pages,
  186. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  187. };
  188. static struct agp_device_ids via_agp_device_ids[] __devinitdata =
  189. {
  190. {
  191. .device_id = PCI_DEVICE_ID_VIA_82C597_0,
  192. .chipset_name = "Apollo VP3",
  193. },
  194. {
  195. .device_id = PCI_DEVICE_ID_VIA_82C598_0,
  196. .chipset_name = "Apollo MVP3",
  197. },
  198. {
  199. .device_id = PCI_DEVICE_ID_VIA_8501_0,
  200. .chipset_name = "Apollo MVP4",
  201. },
  202. /* VT8601 */
  203. {
  204. .device_id = PCI_DEVICE_ID_VIA_8601_0,
  205. .chipset_name = "Apollo ProMedia/PLE133Ta",
  206. },
  207. /* VT82C693A / VT28C694T */
  208. {
  209. .device_id = PCI_DEVICE_ID_VIA_82C691_0,
  210. .chipset_name = "Apollo Pro 133",
  211. },
  212. {
  213. .device_id = PCI_DEVICE_ID_VIA_8371_0,
  214. .chipset_name = "KX133",
  215. },
  216. /* VT8633 */
  217. {
  218. .device_id = PCI_DEVICE_ID_VIA_8633_0,
  219. .chipset_name = "Pro 266",
  220. },
  221. {
  222. .device_id = PCI_DEVICE_ID_VIA_XN266,
  223. .chipset_name = "Apollo Pro266",
  224. },
  225. /* VT8361 */
  226. {
  227. .device_id = PCI_DEVICE_ID_VIA_8361,
  228. .chipset_name = "KLE133",
  229. },
  230. /* VT8365 / VT8362 */
  231. {
  232. .device_id = PCI_DEVICE_ID_VIA_8363_0,
  233. .chipset_name = "Twister-K/KT133x/KM133",
  234. },
  235. /* VT8753A */
  236. {
  237. .device_id = PCI_DEVICE_ID_VIA_8753_0,
  238. .chipset_name = "P4X266",
  239. },
  240. /* VT8366 */
  241. {
  242. .device_id = PCI_DEVICE_ID_VIA_8367_0,
  243. .chipset_name = "KT266/KY266x/KT333",
  244. },
  245. /* VT8633 (for CuMine/ Celeron) */
  246. {
  247. .device_id = PCI_DEVICE_ID_VIA_8653_0,
  248. .chipset_name = "Pro266T",
  249. },
  250. /* KM266 / PM266 */
  251. {
  252. .device_id = PCI_DEVICE_ID_VIA_XM266,
  253. .chipset_name = "PM266/KM266",
  254. },
  255. /* CLE266 */
  256. {
  257. .device_id = PCI_DEVICE_ID_VIA_862X_0,
  258. .chipset_name = "CLE266",
  259. },
  260. {
  261. .device_id = PCI_DEVICE_ID_VIA_8377_0,
  262. .chipset_name = "KT400/KT400A/KT600",
  263. },
  264. /* VT8604 / VT8605 / VT8603
  265. * (Apollo Pro133A chipset with S3 Savage4) */
  266. {
  267. .device_id = PCI_DEVICE_ID_VIA_8605_0,
  268. .chipset_name = "ProSavage PM133/PL133/PN133"
  269. },
  270. /* P4M266x/P4N266 */
  271. {
  272. .device_id = PCI_DEVICE_ID_VIA_8703_51_0,
  273. .chipset_name = "P4M266x/P4N266",
  274. },
  275. /* VT8754 */
  276. {
  277. .device_id = PCI_DEVICE_ID_VIA_8754C_0,
  278. .chipset_name = "PT800",
  279. },
  280. /* P4X600 */
  281. {
  282. .device_id = PCI_DEVICE_ID_VIA_8763_0,
  283. .chipset_name = "P4X600"
  284. },
  285. /* KM400 */
  286. {
  287. .device_id = PCI_DEVICE_ID_VIA_8378_0,
  288. .chipset_name = "KM400/KM400A",
  289. },
  290. /* PT880 */
  291. {
  292. .device_id = PCI_DEVICE_ID_VIA_PT880,
  293. .chipset_name = "PT880",
  294. },
  295. /* PT880 Ultra */
  296. {
  297. .device_id = PCI_DEVICE_ID_VIA_PT880ULTRA,
  298. .chipset_name = "PT880 Ultra",
  299. },
  300. /* PT890 */
  301. {
  302. .device_id = PCI_DEVICE_ID_VIA_8783_0,
  303. .chipset_name = "PT890",
  304. },
  305. /* PM800/PN800/PM880/PN880 */
  306. {
  307. .device_id = PCI_DEVICE_ID_VIA_PX8X0_0,
  308. .chipset_name = "PM800/PN800/PM880/PN880",
  309. },
  310. /* KT880 */
  311. {
  312. .device_id = PCI_DEVICE_ID_VIA_3269_0,
  313. .chipset_name = "KT880",
  314. },
  315. /* KTxxx/Px8xx */
  316. {
  317. .device_id = PCI_DEVICE_ID_VIA_83_87XX_1,
  318. .chipset_name = "VT83xx/VT87xx/KTxxx/Px8xx",
  319. },
  320. /* P4M800 */
  321. {
  322. .device_id = PCI_DEVICE_ID_VIA_3296_0,
  323. .chipset_name = "P4M800",
  324. },
  325. /* P4M800CE */
  326. {
  327. .device_id = PCI_DEVICE_ID_VIA_P4M800CE,
  328. .chipset_name = "VT3314",
  329. },
  330. /* VT3324 / CX700 */
  331. {
  332. .device_id = PCI_DEVICE_ID_VIA_VT3324,
  333. .chipset_name = "CX700",
  334. },
  335. /* VT3336 - this is a chipset for AMD Athlon/K8 CPU. Due to K8's unique
  336. * architecture, the AGP resource and behavior are different from
  337. * the traditional AGP which resides only in chipset. AGP is used
  338. * by 3D driver which wasn't available for the VT3336 and VT3364
  339. * generation until now. Unfortunately, by testing, VT3364 works
  340. * but VT3336 doesn't. - explanation from via, just leave this as
  341. * as a placeholder to avoid future patches adding it back in.
  342. */
  343. #if 0
  344. {
  345. .device_id = PCI_DEVICE_ID_VIA_VT3336,
  346. .chipset_name = "VT3336",
  347. },
  348. #endif
  349. /* P4M890 */
  350. {
  351. .device_id = PCI_DEVICE_ID_VIA_P4M890,
  352. .chipset_name = "P4M890",
  353. },
  354. /* P4M900 */
  355. {
  356. .device_id = PCI_DEVICE_ID_VIA_VT3364,
  357. .chipset_name = "P4M900",
  358. },
  359. { }, /* dummy final entry, always present */
  360. };
  361. /*
  362. * VIA's AGP3 chipsets do magick to put the AGP bridge compliant
  363. * with the same standards version as the graphics card.
  364. */
  365. static void check_via_agp3 (struct agp_bridge_data *bridge)
  366. {
  367. u8 reg;
  368. pci_read_config_byte(bridge->dev, VIA_AGPSEL, &reg);
  369. /* Check AGP 2.0 compatibility mode. */
  370. if ((reg & (1<<1))==0)
  371. bridge->driver = &via_agp3_driver;
  372. }
  373. static int __devinit agp_via_probe(struct pci_dev *pdev,
  374. const struct pci_device_id *ent)
  375. {
  376. struct agp_device_ids *devs = via_agp_device_ids;
  377. struct agp_bridge_data *bridge;
  378. int j = 0;
  379. u8 cap_ptr;
  380. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  381. if (!cap_ptr)
  382. return -ENODEV;
  383. j = ent - agp_via_pci_table;
  384. printk (KERN_INFO PFX "Detected VIA %s chipset\n", devs[j].chipset_name);
  385. bridge = agp_alloc_bridge();
  386. if (!bridge)
  387. return -ENOMEM;
  388. bridge->dev = pdev;
  389. bridge->capndx = cap_ptr;
  390. bridge->driver = &via_driver;
  391. /*
  392. * Garg, there are KT400s with KT266 IDs.
  393. */
  394. if (pdev->device == PCI_DEVICE_ID_VIA_8367_0) {
  395. /* Is there a KT400 subsystem ? */
  396. if (pdev->subsystem_device == PCI_DEVICE_ID_VIA_8377_0) {
  397. printk(KERN_INFO PFX "Found KT400 in disguise as a KT266.\n");
  398. check_via_agp3(bridge);
  399. }
  400. }
  401. /* If this is an AGP3 bridge, check which mode its in and adjust. */
  402. get_agp_version(bridge);
  403. if (bridge->major_version >= 3)
  404. check_via_agp3(bridge);
  405. /* Fill in the mode register */
  406. pci_read_config_dword(pdev,
  407. bridge->capndx+PCI_AGP_STATUS, &bridge->mode);
  408. pci_set_drvdata(pdev, bridge);
  409. return agp_add_bridge(bridge);
  410. }
  411. static void __devexit agp_via_remove(struct pci_dev *pdev)
  412. {
  413. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  414. agp_remove_bridge(bridge);
  415. agp_put_bridge(bridge);
  416. }
  417. #ifdef CONFIG_PM
  418. static int agp_via_suspend(struct pci_dev *pdev, pm_message_t state)
  419. {
  420. pci_save_state (pdev);
  421. pci_set_power_state (pdev, PCI_D3hot);
  422. return 0;
  423. }
  424. static int agp_via_resume(struct pci_dev *pdev)
  425. {
  426. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  427. pci_set_power_state (pdev, PCI_D0);
  428. pci_restore_state(pdev);
  429. if (bridge->driver == &via_agp3_driver)
  430. return via_configure_agp3();
  431. else if (bridge->driver == &via_driver)
  432. return via_configure();
  433. return 0;
  434. }
  435. #endif /* CONFIG_PM */
  436. /* must be the same order as name table above */
  437. static const struct pci_device_id agp_via_pci_table[] = {
  438. #define ID(x) \
  439. { \
  440. .class = (PCI_CLASS_BRIDGE_HOST << 8), \
  441. .class_mask = ~0, \
  442. .vendor = PCI_VENDOR_ID_VIA, \
  443. .device = x, \
  444. .subvendor = PCI_ANY_ID, \
  445. .subdevice = PCI_ANY_ID, \
  446. }
  447. ID(PCI_DEVICE_ID_VIA_82C597_0),
  448. ID(PCI_DEVICE_ID_VIA_82C598_0),
  449. ID(PCI_DEVICE_ID_VIA_8501_0),
  450. ID(PCI_DEVICE_ID_VIA_8601_0),
  451. ID(PCI_DEVICE_ID_VIA_82C691_0),
  452. ID(PCI_DEVICE_ID_VIA_8371_0),
  453. ID(PCI_DEVICE_ID_VIA_8633_0),
  454. ID(PCI_DEVICE_ID_VIA_XN266),
  455. ID(PCI_DEVICE_ID_VIA_8361),
  456. ID(PCI_DEVICE_ID_VIA_8363_0),
  457. ID(PCI_DEVICE_ID_VIA_8753_0),
  458. ID(PCI_DEVICE_ID_VIA_8367_0),
  459. ID(PCI_DEVICE_ID_VIA_8653_0),
  460. ID(PCI_DEVICE_ID_VIA_XM266),
  461. ID(PCI_DEVICE_ID_VIA_862X_0),
  462. ID(PCI_DEVICE_ID_VIA_8377_0),
  463. ID(PCI_DEVICE_ID_VIA_8605_0),
  464. ID(PCI_DEVICE_ID_VIA_8703_51_0),
  465. ID(PCI_DEVICE_ID_VIA_8754C_0),
  466. ID(PCI_DEVICE_ID_VIA_8763_0),
  467. ID(PCI_DEVICE_ID_VIA_8378_0),
  468. ID(PCI_DEVICE_ID_VIA_PT880),
  469. ID(PCI_DEVICE_ID_VIA_PT880ULTRA),
  470. ID(PCI_DEVICE_ID_VIA_8783_0),
  471. ID(PCI_DEVICE_ID_VIA_PX8X0_0),
  472. ID(PCI_DEVICE_ID_VIA_3269_0),
  473. ID(PCI_DEVICE_ID_VIA_83_87XX_1),
  474. ID(PCI_DEVICE_ID_VIA_3296_0),
  475. ID(PCI_DEVICE_ID_VIA_P4M800CE),
  476. ID(PCI_DEVICE_ID_VIA_VT3324),
  477. ID(PCI_DEVICE_ID_VIA_P4M890),
  478. ID(PCI_DEVICE_ID_VIA_VT3364),
  479. { }
  480. };
  481. MODULE_DEVICE_TABLE(pci, agp_via_pci_table);
  482. static struct pci_driver agp_via_pci_driver = {
  483. .name = "agpgart-via",
  484. .id_table = agp_via_pci_table,
  485. .probe = agp_via_probe,
  486. .remove = agp_via_remove,
  487. #ifdef CONFIG_PM
  488. .suspend = agp_via_suspend,
  489. .resume = agp_via_resume,
  490. #endif
  491. };
  492. static int __init agp_via_init(void)
  493. {
  494. if (agp_off)
  495. return -EINVAL;
  496. return pci_register_driver(&agp_via_pci_driver);
  497. }
  498. static void __exit agp_via_cleanup(void)
  499. {
  500. pci_unregister_driver(&agp_via_pci_driver);
  501. }
  502. module_init(agp_via_init);
  503. module_exit(agp_via_cleanup);
  504. MODULE_LICENSE("GPL");
  505. MODULE_AUTHOR("Dave Jones <davej@redhat.com>");