via-agp.c 14 KB

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