via-agp.c 14 KB

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