via-agp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. .configure = via_configure_agp3,
  142. .fetch_size = via_fetch_size_agp3,
  143. .cleanup = via_cleanup_agp3,
  144. .tlb_flush = via_tlbflush_agp3,
  145. .mask_memory = agp_generic_mask_memory,
  146. .masks = NULL,
  147. .agp_enable = agp_generic_enable,
  148. .cache_flush = global_cache_flush,
  149. .create_gatt_table = agp_generic_create_gatt_table,
  150. .free_gatt_table = agp_generic_free_gatt_table,
  151. .insert_memory = agp_generic_insert_memory,
  152. .remove_memory = agp_generic_remove_memory,
  153. .alloc_by_type = agp_generic_alloc_by_type,
  154. .free_by_type = agp_generic_free_by_type,
  155. .agp_alloc_page = agp_generic_alloc_page,
  156. .agp_destroy_page = agp_generic_destroy_page,
  157. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  158. };
  159. static const struct agp_bridge_driver via_driver = {
  160. .owner = THIS_MODULE,
  161. .aperture_sizes = via_generic_sizes,
  162. .size_type = U8_APER_SIZE,
  163. .num_aperture_sizes = 9,
  164. .configure = via_configure,
  165. .fetch_size = via_fetch_size,
  166. .cleanup = via_cleanup,
  167. .tlb_flush = via_tlbflush,
  168. .mask_memory = agp_generic_mask_memory,
  169. .masks = NULL,
  170. .agp_enable = agp_generic_enable,
  171. .cache_flush = global_cache_flush,
  172. .create_gatt_table = agp_generic_create_gatt_table,
  173. .free_gatt_table = agp_generic_free_gatt_table,
  174. .insert_memory = agp_generic_insert_memory,
  175. .remove_memory = agp_generic_remove_memory,
  176. .alloc_by_type = agp_generic_alloc_by_type,
  177. .free_by_type = agp_generic_free_by_type,
  178. .agp_alloc_page = agp_generic_alloc_page,
  179. .agp_destroy_page = agp_generic_destroy_page,
  180. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  181. };
  182. static struct agp_device_ids via_agp_device_ids[] __devinitdata =
  183. {
  184. {
  185. .device_id = PCI_DEVICE_ID_VIA_82C597_0,
  186. .chipset_name = "Apollo VP3",
  187. },
  188. {
  189. .device_id = PCI_DEVICE_ID_VIA_82C598_0,
  190. .chipset_name = "Apollo MVP3",
  191. },
  192. {
  193. .device_id = PCI_DEVICE_ID_VIA_8501_0,
  194. .chipset_name = "Apollo MVP4",
  195. },
  196. /* VT8601 */
  197. {
  198. .device_id = PCI_DEVICE_ID_VIA_8601_0,
  199. .chipset_name = "Apollo ProMedia/PLE133Ta",
  200. },
  201. /* VT82C693A / VT28C694T */
  202. {
  203. .device_id = PCI_DEVICE_ID_VIA_82C691_0,
  204. .chipset_name = "Apollo Pro 133",
  205. },
  206. {
  207. .device_id = PCI_DEVICE_ID_VIA_8371_0,
  208. .chipset_name = "KX133",
  209. },
  210. /* VT8633 */
  211. {
  212. .device_id = PCI_DEVICE_ID_VIA_8633_0,
  213. .chipset_name = "Pro 266",
  214. },
  215. {
  216. .device_id = PCI_DEVICE_ID_VIA_XN266,
  217. .chipset_name = "Apollo Pro266",
  218. },
  219. /* VT8361 */
  220. {
  221. .device_id = PCI_DEVICE_ID_VIA_8361,
  222. .chipset_name = "KLE133",
  223. },
  224. /* VT8365 / VT8362 */
  225. {
  226. .device_id = PCI_DEVICE_ID_VIA_8363_0,
  227. .chipset_name = "Twister-K/KT133x/KM133",
  228. },
  229. /* VT8753A */
  230. {
  231. .device_id = PCI_DEVICE_ID_VIA_8753_0,
  232. .chipset_name = "P4X266",
  233. },
  234. /* VT8366 */
  235. {
  236. .device_id = PCI_DEVICE_ID_VIA_8367_0,
  237. .chipset_name = "KT266/KY266x/KT333",
  238. },
  239. /* VT8633 (for CuMine/ Celeron) */
  240. {
  241. .device_id = PCI_DEVICE_ID_VIA_8653_0,
  242. .chipset_name = "Pro266T",
  243. },
  244. /* KM266 / PM266 */
  245. {
  246. .device_id = PCI_DEVICE_ID_VIA_XM266,
  247. .chipset_name = "PM266/KM266",
  248. },
  249. /* CLE266 */
  250. {
  251. .device_id = PCI_DEVICE_ID_VIA_862X_0,
  252. .chipset_name = "CLE266",
  253. },
  254. {
  255. .device_id = PCI_DEVICE_ID_VIA_8377_0,
  256. .chipset_name = "KT400/KT400A/KT600",
  257. },
  258. /* VT8604 / VT8605 / VT8603
  259. * (Apollo Pro133A chipset with S3 Savage4) */
  260. {
  261. .device_id = PCI_DEVICE_ID_VIA_8605_0,
  262. .chipset_name = "ProSavage PM133/PL133/PN133"
  263. },
  264. /* P4M266x/P4N266 */
  265. {
  266. .device_id = PCI_DEVICE_ID_VIA_8703_51_0,
  267. .chipset_name = "P4M266x/P4N266",
  268. },
  269. /* VT8754 */
  270. {
  271. .device_id = PCI_DEVICE_ID_VIA_8754C_0,
  272. .chipset_name = "PT800",
  273. },
  274. /* P4X600 */
  275. {
  276. .device_id = PCI_DEVICE_ID_VIA_8763_0,
  277. .chipset_name = "P4X600"
  278. },
  279. /* KM400 */
  280. {
  281. .device_id = PCI_DEVICE_ID_VIA_8378_0,
  282. .chipset_name = "KM400/KM400A",
  283. },
  284. /* PT880 */
  285. {
  286. .device_id = PCI_DEVICE_ID_VIA_PT880,
  287. .chipset_name = "PT880",
  288. },
  289. /* PT880 Ultra */
  290. {
  291. .device_id = PCI_DEVICE_ID_VIA_PT880ULTRA,
  292. .chipset_name = "PT880 Ultra",
  293. },
  294. /* PT890 */
  295. {
  296. .device_id = PCI_DEVICE_ID_VIA_8783_0,
  297. .chipset_name = "PT890",
  298. },
  299. /* PM800/PN800/PM880/PN880 */
  300. {
  301. .device_id = PCI_DEVICE_ID_VIA_PX8X0_0,
  302. .chipset_name = "PM800/PN800/PM880/PN880",
  303. },
  304. /* KT880 */
  305. {
  306. .device_id = PCI_DEVICE_ID_VIA_3269_0,
  307. .chipset_name = "KT880",
  308. },
  309. /* KTxxx/Px8xx */
  310. {
  311. .device_id = PCI_DEVICE_ID_VIA_83_87XX_1,
  312. .chipset_name = "VT83xx/VT87xx/KTxxx/Px8xx",
  313. },
  314. /* P4M800 */
  315. {
  316. .device_id = PCI_DEVICE_ID_VIA_3296_0,
  317. .chipset_name = "P4M800",
  318. },
  319. /* P4M800CE */
  320. {
  321. .device_id = PCI_DEVICE_ID_VIA_P4M800CE,
  322. .chipset_name = "VT3314",
  323. },
  324. /* VT3324 / CX700 */
  325. {
  326. .device_id = PCI_DEVICE_ID_VIA_VT3324,
  327. .chipset_name = "CX700",
  328. },
  329. /* VT3336 */
  330. {
  331. .device_id = PCI_DEVICE_ID_VIA_VT3336,
  332. .chipset_name = "VT3336",
  333. },
  334. /* P4M890 */
  335. {
  336. .device_id = PCI_DEVICE_ID_VIA_P4M890,
  337. .chipset_name = "P4M890",
  338. },
  339. { }, /* dummy final entry, always present */
  340. };
  341. /*
  342. * VIA's AGP3 chipsets do magick to put the AGP bridge compliant
  343. * with the same standards version as the graphics card.
  344. */
  345. static void check_via_agp3 (struct agp_bridge_data *bridge)
  346. {
  347. u8 reg;
  348. pci_read_config_byte(bridge->dev, VIA_AGPSEL, &reg);
  349. /* Check AGP 2.0 compatibility mode. */
  350. if ((reg & (1<<1))==0)
  351. bridge->driver = &via_agp3_driver;
  352. }
  353. static int __devinit agp_via_probe(struct pci_dev *pdev,
  354. const struct pci_device_id *ent)
  355. {
  356. struct agp_device_ids *devs = via_agp_device_ids;
  357. struct agp_bridge_data *bridge;
  358. int j = 0;
  359. u8 cap_ptr;
  360. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  361. if (!cap_ptr)
  362. return -ENODEV;
  363. j = ent - agp_via_pci_table;
  364. printk (KERN_INFO PFX "Detected VIA %s chipset\n", devs[j].chipset_name);
  365. bridge = agp_alloc_bridge();
  366. if (!bridge)
  367. return -ENOMEM;
  368. bridge->dev = pdev;
  369. bridge->capndx = cap_ptr;
  370. bridge->driver = &via_driver;
  371. /*
  372. * Garg, there are KT400s with KT266 IDs.
  373. */
  374. if (pdev->device == PCI_DEVICE_ID_VIA_8367_0) {
  375. /* Is there a KT400 subsystem ? */
  376. if (pdev->subsystem_device == PCI_DEVICE_ID_VIA_8377_0) {
  377. printk(KERN_INFO PFX "Found KT400 in disguise as a KT266.\n");
  378. check_via_agp3(bridge);
  379. }
  380. }
  381. /* If this is an AGP3 bridge, check which mode its in and adjust. */
  382. get_agp_version(bridge);
  383. if (bridge->major_version >= 3)
  384. check_via_agp3(bridge);
  385. /* Fill in the mode register */
  386. pci_read_config_dword(pdev,
  387. bridge->capndx+PCI_AGP_STATUS, &bridge->mode);
  388. pci_set_drvdata(pdev, bridge);
  389. return agp_add_bridge(bridge);
  390. }
  391. static void __devexit agp_via_remove(struct pci_dev *pdev)
  392. {
  393. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  394. agp_remove_bridge(bridge);
  395. agp_put_bridge(bridge);
  396. }
  397. #ifdef CONFIG_PM
  398. static int agp_via_suspend(struct pci_dev *pdev, pm_message_t state)
  399. {
  400. pci_save_state (pdev);
  401. pci_set_power_state (pdev, PCI_D3hot);
  402. return 0;
  403. }
  404. static int agp_via_resume(struct pci_dev *pdev)
  405. {
  406. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  407. pci_set_power_state (pdev, PCI_D0);
  408. pci_restore_state(pdev);
  409. if (bridge->driver == &via_agp3_driver)
  410. return via_configure_agp3();
  411. else if (bridge->driver == &via_driver)
  412. return via_configure();
  413. return 0;
  414. }
  415. #endif /* CONFIG_PM */
  416. /* must be the same order as name table above */
  417. static const struct pci_device_id agp_via_pci_table[] = {
  418. #define ID(x) \
  419. { \
  420. .class = (PCI_CLASS_BRIDGE_HOST << 8), \
  421. .class_mask = ~0, \
  422. .vendor = PCI_VENDOR_ID_VIA, \
  423. .device = x, \
  424. .subvendor = PCI_ANY_ID, \
  425. .subdevice = PCI_ANY_ID, \
  426. }
  427. ID(PCI_DEVICE_ID_VIA_82C597_0),
  428. ID(PCI_DEVICE_ID_VIA_82C598_0),
  429. ID(PCI_DEVICE_ID_VIA_8501_0),
  430. ID(PCI_DEVICE_ID_VIA_8601_0),
  431. ID(PCI_DEVICE_ID_VIA_82C691_0),
  432. ID(PCI_DEVICE_ID_VIA_8371_0),
  433. ID(PCI_DEVICE_ID_VIA_8633_0),
  434. ID(PCI_DEVICE_ID_VIA_XN266),
  435. ID(PCI_DEVICE_ID_VIA_8361),
  436. ID(PCI_DEVICE_ID_VIA_8363_0),
  437. ID(PCI_DEVICE_ID_VIA_8753_0),
  438. ID(PCI_DEVICE_ID_VIA_8367_0),
  439. ID(PCI_DEVICE_ID_VIA_8653_0),
  440. ID(PCI_DEVICE_ID_VIA_XM266),
  441. ID(PCI_DEVICE_ID_VIA_862X_0),
  442. ID(PCI_DEVICE_ID_VIA_8377_0),
  443. ID(PCI_DEVICE_ID_VIA_8605_0),
  444. ID(PCI_DEVICE_ID_VIA_8703_51_0),
  445. ID(PCI_DEVICE_ID_VIA_8754C_0),
  446. ID(PCI_DEVICE_ID_VIA_8763_0),
  447. ID(PCI_DEVICE_ID_VIA_8378_0),
  448. ID(PCI_DEVICE_ID_VIA_PT880),
  449. ID(PCI_DEVICE_ID_VIA_PT880ULTRA),
  450. ID(PCI_DEVICE_ID_VIA_8783_0),
  451. ID(PCI_DEVICE_ID_VIA_PX8X0_0),
  452. ID(PCI_DEVICE_ID_VIA_3269_0),
  453. ID(PCI_DEVICE_ID_VIA_83_87XX_1),
  454. ID(PCI_DEVICE_ID_VIA_3296_0),
  455. ID(PCI_DEVICE_ID_VIA_P4M800CE),
  456. ID(PCI_DEVICE_ID_VIA_VT3324),
  457. ID(PCI_DEVICE_ID_VIA_VT3336),
  458. ID(PCI_DEVICE_ID_VIA_P4M890),
  459. { }
  460. };
  461. MODULE_DEVICE_TABLE(pci, agp_via_pci_table);
  462. static struct pci_driver agp_via_pci_driver = {
  463. .name = "agpgart-via",
  464. .id_table = agp_via_pci_table,
  465. .probe = agp_via_probe,
  466. .remove = agp_via_remove,
  467. #ifdef CONFIG_PM
  468. .suspend = agp_via_suspend,
  469. .resume = agp_via_resume,
  470. #endif
  471. };
  472. static int __init agp_via_init(void)
  473. {
  474. if (agp_off)
  475. return -EINVAL;
  476. return pci_register_driver(&agp_via_pci_driver);
  477. }
  478. static void __exit agp_via_cleanup(void)
  479. {
  480. pci_unregister_driver(&agp_via_pci_driver);
  481. }
  482. module_init(agp_via_init);
  483. module_exit(agp_via_cleanup);
  484. MODULE_LICENSE("GPL");
  485. MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");