sis-agp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * SiS AGPGART routines.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/pci.h>
  6. #include <linux/init.h>
  7. #include <linux/agp_backend.h>
  8. #include <linux/delay.h>
  9. #include "agp.h"
  10. #define SIS_ATTBASE 0x90
  11. #define SIS_APSIZE 0x94
  12. #define SIS_TLBCNTRL 0x97
  13. #define SIS_TLBFLUSH 0x98
  14. static int __devinitdata agp_sis_force_delay = 0;
  15. static int __devinitdata agp_sis_agp_spec = -1;
  16. static int sis_fetch_size(void)
  17. {
  18. u8 temp_size;
  19. int i;
  20. struct aper_size_info_8 *values;
  21. pci_read_config_byte(agp_bridge->dev, SIS_APSIZE, &temp_size);
  22. values = A_SIZE_8(agp_bridge->driver->aperture_sizes);
  23. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  24. if ((temp_size == values[i].size_value) ||
  25. ((temp_size & ~(0x03)) ==
  26. (values[i].size_value & ~(0x03)))) {
  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. return 0;
  34. }
  35. static void sis_tlbflush(struct agp_memory *mem)
  36. {
  37. pci_write_config_byte(agp_bridge->dev, SIS_TLBFLUSH, 0x02);
  38. }
  39. static int sis_configure(void)
  40. {
  41. u32 temp;
  42. struct aper_size_info_8 *current_size;
  43. current_size = A_SIZE_8(agp_bridge->current_size);
  44. pci_write_config_byte(agp_bridge->dev, SIS_TLBCNTRL, 0x05);
  45. pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
  46. agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  47. pci_write_config_dword(agp_bridge->dev, SIS_ATTBASE,
  48. agp_bridge->gatt_bus_addr);
  49. pci_write_config_byte(agp_bridge->dev, SIS_APSIZE,
  50. current_size->size_value);
  51. return 0;
  52. }
  53. static void sis_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, SIS_APSIZE,
  58. (previous_size->size_value & ~(0x03)));
  59. }
  60. static void sis_delayed_enable(struct agp_bridge_data *bridge, u32 mode)
  61. {
  62. struct pci_dev *device = NULL;
  63. u32 command;
  64. int rate;
  65. printk(KERN_INFO PFX "Found an AGP %d.%d compliant device at %s.\n",
  66. agp_bridge->major_version,
  67. agp_bridge->minor_version,
  68. pci_name(agp_bridge->dev));
  69. pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx + PCI_AGP_STATUS, &command);
  70. command = agp_collect_device_status(bridge, mode, command);
  71. command |= AGPSTAT_AGP_ENABLE;
  72. rate = (command & 0x7) << 2;
  73. for_each_pci_dev(device) {
  74. u8 agp = pci_find_capability(device, PCI_CAP_ID_AGP);
  75. if (!agp)
  76. continue;
  77. printk(KERN_INFO PFX "Putting AGP V3 device at %s into %dx mode\n",
  78. pci_name(device), rate);
  79. pci_write_config_dword(device, agp + PCI_AGP_COMMAND, command);
  80. /*
  81. * Weird: on some sis chipsets any rate change in the target
  82. * command register triggers a 5ms screwup during which the master
  83. * cannot be configured
  84. */
  85. if (device->device == bridge->dev->device) {
  86. printk(KERN_INFO PFX "SiS delay workaround: giving bridge time to recover.\n");
  87. msleep(10);
  88. }
  89. }
  90. }
  91. static const struct aper_size_info_8 sis_generic_sizes[7] =
  92. {
  93. {256, 65536, 6, 99},
  94. {128, 32768, 5, 83},
  95. {64, 16384, 4, 67},
  96. {32, 8192, 3, 51},
  97. {16, 4096, 2, 35},
  98. {8, 2048, 1, 19},
  99. {4, 1024, 0, 3}
  100. };
  101. static struct agp_bridge_driver sis_driver = {
  102. .owner = THIS_MODULE,
  103. .aperture_sizes = sis_generic_sizes,
  104. .size_type = U8_APER_SIZE,
  105. .num_aperture_sizes = 7,
  106. .configure = sis_configure,
  107. .fetch_size = sis_fetch_size,
  108. .cleanup = sis_cleanup,
  109. .tlb_flush = sis_tlbflush,
  110. .mask_memory = agp_generic_mask_memory,
  111. .masks = NULL,
  112. .agp_enable = agp_generic_enable,
  113. .cache_flush = global_cache_flush,
  114. .create_gatt_table = agp_generic_create_gatt_table,
  115. .free_gatt_table = agp_generic_free_gatt_table,
  116. .insert_memory = agp_generic_insert_memory,
  117. .remove_memory = agp_generic_remove_memory,
  118. .alloc_by_type = agp_generic_alloc_by_type,
  119. .free_by_type = agp_generic_free_by_type,
  120. .agp_alloc_page = agp_generic_alloc_page,
  121. .agp_destroy_page = agp_generic_destroy_page,
  122. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  123. };
  124. static struct agp_device_ids sis_agp_device_ids[] __devinitdata =
  125. {
  126. {
  127. .device_id = PCI_DEVICE_ID_SI_5591_AGP,
  128. .chipset_name = "5591",
  129. },
  130. {
  131. .device_id = PCI_DEVICE_ID_SI_530,
  132. .chipset_name = "530",
  133. },
  134. {
  135. .device_id = PCI_DEVICE_ID_SI_540,
  136. .chipset_name = "540",
  137. },
  138. {
  139. .device_id = PCI_DEVICE_ID_SI_550,
  140. .chipset_name = "550",
  141. },
  142. {
  143. .device_id = PCI_DEVICE_ID_SI_620,
  144. .chipset_name = "620",
  145. },
  146. {
  147. .device_id = PCI_DEVICE_ID_SI_630,
  148. .chipset_name = "630",
  149. },
  150. {
  151. .device_id = PCI_DEVICE_ID_SI_635,
  152. .chipset_name = "635",
  153. },
  154. {
  155. .device_id = PCI_DEVICE_ID_SI_645,
  156. .chipset_name = "645",
  157. },
  158. {
  159. .device_id = PCI_DEVICE_ID_SI_646,
  160. .chipset_name = "646",
  161. },
  162. {
  163. .device_id = PCI_DEVICE_ID_SI_648,
  164. .chipset_name = "648",
  165. },
  166. {
  167. .device_id = PCI_DEVICE_ID_SI_650,
  168. .chipset_name = "650",
  169. },
  170. {
  171. .device_id = PCI_DEVICE_ID_SI_651,
  172. .chipset_name = "651",
  173. },
  174. {
  175. .device_id = PCI_DEVICE_ID_SI_655,
  176. .chipset_name = "655",
  177. },
  178. {
  179. .device_id = PCI_DEVICE_ID_SI_661,
  180. .chipset_name = "661",
  181. },
  182. {
  183. .device_id = PCI_DEVICE_ID_SI_730,
  184. .chipset_name = "730",
  185. },
  186. {
  187. .device_id = PCI_DEVICE_ID_SI_735,
  188. .chipset_name = "735",
  189. },
  190. {
  191. .device_id = PCI_DEVICE_ID_SI_740,
  192. .chipset_name = "740",
  193. },
  194. {
  195. .device_id = PCI_DEVICE_ID_SI_741,
  196. .chipset_name = "741",
  197. },
  198. {
  199. .device_id = PCI_DEVICE_ID_SI_745,
  200. .chipset_name = "745",
  201. },
  202. {
  203. .device_id = PCI_DEVICE_ID_SI_746,
  204. .chipset_name = "746",
  205. },
  206. {
  207. .device_id = PCI_DEVICE_ID_SI_760,
  208. .chipset_name = "760",
  209. },
  210. { }, /* dummy final entry, always present */
  211. };
  212. // chipsets that require the 'delay hack'
  213. static int sis_broken_chipsets[] __devinitdata = {
  214. PCI_DEVICE_ID_SI_648,
  215. PCI_DEVICE_ID_SI_746,
  216. 0 // terminator
  217. };
  218. static void __devinit sis_get_driver(struct agp_bridge_data *bridge)
  219. {
  220. int i;
  221. for (i=0; sis_broken_chipsets[i]!=0; ++i)
  222. if (bridge->dev->device==sis_broken_chipsets[i])
  223. break;
  224. if (sis_broken_chipsets[i] || agp_sis_force_delay)
  225. sis_driver.agp_enable=sis_delayed_enable;
  226. // sis chipsets that indicate less than agp3.5
  227. // are not actually fully agp3 compliant
  228. if ((agp_bridge->major_version == 3 && agp_bridge->minor_version >= 5
  229. && agp_sis_agp_spec!=0) || agp_sis_agp_spec==1) {
  230. sis_driver.aperture_sizes = agp3_generic_sizes;
  231. sis_driver.size_type = U16_APER_SIZE;
  232. sis_driver.num_aperture_sizes = AGP_GENERIC_SIZES_ENTRIES;
  233. sis_driver.configure = agp3_generic_configure;
  234. sis_driver.fetch_size = agp3_generic_fetch_size;
  235. sis_driver.cleanup = agp3_generic_cleanup;
  236. sis_driver.tlb_flush = agp3_generic_tlbflush;
  237. }
  238. }
  239. static int __devinit agp_sis_probe(struct pci_dev *pdev,
  240. const struct pci_device_id *ent)
  241. {
  242. struct agp_device_ids *devs = sis_agp_device_ids;
  243. struct agp_bridge_data *bridge;
  244. u8 cap_ptr;
  245. int j;
  246. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  247. if (!cap_ptr)
  248. return -ENODEV;
  249. /* probe for known chipsets */
  250. for (j = 0; devs[j].chipset_name; j++) {
  251. if (pdev->device == devs[j].device_id) {
  252. printk(KERN_INFO PFX "Detected SiS %s chipset\n",
  253. devs[j].chipset_name);
  254. goto found;
  255. }
  256. }
  257. printk(KERN_ERR PFX "Unsupported SiS chipset (device id: %04x)\n",
  258. pdev->device);
  259. return -ENODEV;
  260. found:
  261. bridge = agp_alloc_bridge();
  262. if (!bridge)
  263. return -ENOMEM;
  264. bridge->driver = &sis_driver;
  265. bridge->dev = pdev;
  266. bridge->capndx = cap_ptr;
  267. get_agp_version(bridge);
  268. /* Fill in the mode register */
  269. pci_read_config_dword(pdev, bridge->capndx+PCI_AGP_STATUS, &bridge->mode);
  270. sis_get_driver(bridge);
  271. pci_set_drvdata(pdev, bridge);
  272. return agp_add_bridge(bridge);
  273. }
  274. static void __devexit agp_sis_remove(struct pci_dev *pdev)
  275. {
  276. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  277. agp_remove_bridge(bridge);
  278. agp_put_bridge(bridge);
  279. }
  280. static struct pci_device_id agp_sis_pci_table[] = {
  281. {
  282. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  283. .class_mask = ~0,
  284. .vendor = PCI_VENDOR_ID_SI,
  285. .device = PCI_ANY_ID,
  286. .subvendor = PCI_ANY_ID,
  287. .subdevice = PCI_ANY_ID,
  288. },
  289. { }
  290. };
  291. MODULE_DEVICE_TABLE(pci, agp_sis_pci_table);
  292. static struct pci_driver agp_sis_pci_driver = {
  293. .name = "agpgart-sis",
  294. .id_table = agp_sis_pci_table,
  295. .probe = agp_sis_probe,
  296. .remove = agp_sis_remove,
  297. };
  298. static int __init agp_sis_init(void)
  299. {
  300. if (agp_off)
  301. return -EINVAL;
  302. return pci_register_driver(&agp_sis_pci_driver);
  303. }
  304. static void __exit agp_sis_cleanup(void)
  305. {
  306. pci_unregister_driver(&agp_sis_pci_driver);
  307. }
  308. module_init(agp_sis_init);
  309. module_exit(agp_sis_cleanup);
  310. module_param(agp_sis_force_delay, bool, 0);
  311. MODULE_PARM_DESC(agp_sis_force_delay,"forces sis delay hack");
  312. module_param(agp_sis_agp_spec, int, 0);
  313. MODULE_PARM_DESC(agp_sis_agp_spec,"0=force sis init, 1=force generic agp3 init, default: autodetect");
  314. MODULE_LICENSE("GPL and additional rights");