via-agp.c 13 KB

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