nvidia-agp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Nvidia AGPGART routines.
  3. * Based upon a 2.4 agpgart diff by the folks from NVIDIA, and hacked up
  4. * to work in 2.5 by Dave Jones.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/pci.h>
  8. #include <linux/init.h>
  9. #include <linux/agp_backend.h>
  10. #include <linux/page-flags.h>
  11. #include <linux/mm.h>
  12. #include <linux/jiffies.h>
  13. #include "agp.h"
  14. /* NVIDIA registers */
  15. #define NVIDIA_0_APSIZE 0x80
  16. #define NVIDIA_1_WBC 0xf0
  17. #define NVIDIA_2_GARTCTRL 0xd0
  18. #define NVIDIA_2_APBASE 0xd8
  19. #define NVIDIA_2_APLIMIT 0xdc
  20. #define NVIDIA_2_ATTBASE(i) (0xe0 + (i) * 4)
  21. #define NVIDIA_3_APBASE 0x50
  22. #define NVIDIA_3_APLIMIT 0x54
  23. static struct _nvidia_private {
  24. struct pci_dev *dev_1;
  25. struct pci_dev *dev_2;
  26. struct pci_dev *dev_3;
  27. volatile u32 __iomem *aperture;
  28. int num_active_entries;
  29. off_t pg_offset;
  30. u32 wbc_mask;
  31. } nvidia_private;
  32. static int nvidia_fetch_size(void)
  33. {
  34. int i;
  35. u8 size_value;
  36. struct aper_size_info_8 *values;
  37. pci_read_config_byte(agp_bridge->dev, NVIDIA_0_APSIZE, &size_value);
  38. size_value &= 0x0f;
  39. values = A_SIZE_8(agp_bridge->driver->aperture_sizes);
  40. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  41. if (size_value == values[i].size_value) {
  42. agp_bridge->previous_size =
  43. agp_bridge->current_size = (void *) (values + i);
  44. agp_bridge->aperture_size_idx = i;
  45. return values[i].size;
  46. }
  47. }
  48. return 0;
  49. }
  50. #define SYSCFG 0xC0010010
  51. #define IORR_BASE0 0xC0010016
  52. #define IORR_MASK0 0xC0010017
  53. #define AMD_K7_NUM_IORR 2
  54. static int nvidia_init_iorr(u32 base, u32 size)
  55. {
  56. u32 base_hi, base_lo;
  57. u32 mask_hi, mask_lo;
  58. u32 sys_hi, sys_lo;
  59. u32 iorr_addr, free_iorr_addr;
  60. /* Find the iorr that is already used for the base */
  61. /* If not found, determine the uppermost available iorr */
  62. free_iorr_addr = AMD_K7_NUM_IORR;
  63. for (iorr_addr = 0; iorr_addr < AMD_K7_NUM_IORR; iorr_addr++) {
  64. rdmsr(IORR_BASE0 + 2 * iorr_addr, base_lo, base_hi);
  65. rdmsr(IORR_MASK0 + 2 * iorr_addr, mask_lo, mask_hi);
  66. if ((base_lo & 0xfffff000) == (base & 0xfffff000))
  67. break;
  68. if ((mask_lo & 0x00000800) == 0)
  69. free_iorr_addr = iorr_addr;
  70. }
  71. if (iorr_addr >= AMD_K7_NUM_IORR) {
  72. iorr_addr = free_iorr_addr;
  73. if (iorr_addr >= AMD_K7_NUM_IORR)
  74. return -EINVAL;
  75. }
  76. base_hi = 0x0;
  77. base_lo = (base & ~0xfff) | 0x18;
  78. mask_hi = 0xf;
  79. mask_lo = ((~(size - 1)) & 0xfffff000) | 0x800;
  80. wrmsr(IORR_BASE0 + 2 * iorr_addr, base_lo, base_hi);
  81. wrmsr(IORR_MASK0 + 2 * iorr_addr, mask_lo, mask_hi);
  82. rdmsr(SYSCFG, sys_lo, sys_hi);
  83. sys_lo |= 0x00100000;
  84. wrmsr(SYSCFG, sys_lo, sys_hi);
  85. return 0;
  86. }
  87. static int nvidia_configure(void)
  88. {
  89. int i, rc, num_dirs;
  90. u32 apbase, aplimit;
  91. phys_addr_t apbase_phys;
  92. struct aper_size_info_8 *current_size;
  93. u32 temp;
  94. current_size = A_SIZE_8(agp_bridge->current_size);
  95. /* aperture size */
  96. pci_write_config_byte(agp_bridge->dev, NVIDIA_0_APSIZE,
  97. current_size->size_value);
  98. /* address to map to */
  99. apbase = pci_bus_address(agp_bridge->dev, AGP_APERTURE_BAR);
  100. agp_bridge->gart_bus_addr = apbase;
  101. aplimit = apbase + (current_size->size * 1024 * 1024) - 1;
  102. pci_write_config_dword(nvidia_private.dev_2, NVIDIA_2_APBASE, apbase);
  103. pci_write_config_dword(nvidia_private.dev_2, NVIDIA_2_APLIMIT, aplimit);
  104. pci_write_config_dword(nvidia_private.dev_3, NVIDIA_3_APBASE, apbase);
  105. pci_write_config_dword(nvidia_private.dev_3, NVIDIA_3_APLIMIT, aplimit);
  106. if (0 != (rc = nvidia_init_iorr(apbase, current_size->size * 1024 * 1024)))
  107. return rc;
  108. /* directory size is 64k */
  109. num_dirs = current_size->size / 64;
  110. nvidia_private.num_active_entries = current_size->num_entries;
  111. nvidia_private.pg_offset = 0;
  112. if (num_dirs == 0) {
  113. num_dirs = 1;
  114. nvidia_private.num_active_entries /= (64 / current_size->size);
  115. nvidia_private.pg_offset = (apbase & (64 * 1024 * 1024 - 1) &
  116. ~(current_size->size * 1024 * 1024 - 1)) / PAGE_SIZE;
  117. }
  118. /* attbase */
  119. for (i = 0; i < 8; i++) {
  120. pci_write_config_dword(nvidia_private.dev_2, NVIDIA_2_ATTBASE(i),
  121. (agp_bridge->gatt_bus_addr + (i % num_dirs) * 64 * 1024) | 1);
  122. }
  123. /* gtlb control */
  124. pci_read_config_dword(nvidia_private.dev_2, NVIDIA_2_GARTCTRL, &temp);
  125. pci_write_config_dword(nvidia_private.dev_2, NVIDIA_2_GARTCTRL, temp | 0x11);
  126. /* gart control */
  127. pci_read_config_dword(agp_bridge->dev, NVIDIA_0_APSIZE, &temp);
  128. pci_write_config_dword(agp_bridge->dev, NVIDIA_0_APSIZE, temp | 0x100);
  129. /* map aperture */
  130. apbase_phys = pci_resource_start(agp_bridge->dev, AGP_APERTURE_BAR);
  131. nvidia_private.aperture =
  132. (volatile u32 __iomem *) ioremap(apbase_phys, 33 * PAGE_SIZE);
  133. if (!nvidia_private.aperture)
  134. return -ENOMEM;
  135. return 0;
  136. }
  137. static void nvidia_cleanup(void)
  138. {
  139. struct aper_size_info_8 *previous_size;
  140. u32 temp;
  141. /* gart control */
  142. pci_read_config_dword(agp_bridge->dev, NVIDIA_0_APSIZE, &temp);
  143. pci_write_config_dword(agp_bridge->dev, NVIDIA_0_APSIZE, temp & ~(0x100));
  144. /* gtlb control */
  145. pci_read_config_dword(nvidia_private.dev_2, NVIDIA_2_GARTCTRL, &temp);
  146. pci_write_config_dword(nvidia_private.dev_2, NVIDIA_2_GARTCTRL, temp & ~(0x11));
  147. /* unmap aperture */
  148. iounmap((void __iomem *) nvidia_private.aperture);
  149. /* restore previous aperture size */
  150. previous_size = A_SIZE_8(agp_bridge->previous_size);
  151. pci_write_config_byte(agp_bridge->dev, NVIDIA_0_APSIZE,
  152. previous_size->size_value);
  153. /* restore iorr for previous aperture size */
  154. nvidia_init_iorr(agp_bridge->gart_bus_addr,
  155. previous_size->size * 1024 * 1024);
  156. }
  157. /*
  158. * Note we can't use the generic routines, even though they are 99% the same.
  159. * Aperture sizes <64M still requires a full 64k GART directory, but
  160. * only use the portion of the TLB entries that correspond to the apertures
  161. * alignment inside the surrounding 64M block.
  162. */
  163. extern int agp_memory_reserved;
  164. static int nvidia_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
  165. {
  166. int i, j;
  167. int mask_type;
  168. mask_type = agp_generic_type_to_mask_type(mem->bridge, type);
  169. if (mask_type != 0 || type != mem->type)
  170. return -EINVAL;
  171. if (mem->page_count == 0)
  172. return 0;
  173. if ((pg_start + mem->page_count) >
  174. (nvidia_private.num_active_entries - agp_memory_reserved/PAGE_SIZE))
  175. return -EINVAL;
  176. for (j = pg_start; j < (pg_start + mem->page_count); j++) {
  177. if (!PGE_EMPTY(agp_bridge, readl(agp_bridge->gatt_table+nvidia_private.pg_offset+j)))
  178. return -EBUSY;
  179. }
  180. if (!mem->is_flushed) {
  181. global_cache_flush();
  182. mem->is_flushed = true;
  183. }
  184. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  185. writel(agp_bridge->driver->mask_memory(agp_bridge,
  186. page_to_phys(mem->pages[i]), mask_type),
  187. agp_bridge->gatt_table+nvidia_private.pg_offset+j);
  188. }
  189. /* PCI Posting. */
  190. readl(agp_bridge->gatt_table+nvidia_private.pg_offset+j - 1);
  191. agp_bridge->driver->tlb_flush(mem);
  192. return 0;
  193. }
  194. static int nvidia_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
  195. {
  196. int i;
  197. int mask_type;
  198. mask_type = agp_generic_type_to_mask_type(mem->bridge, type);
  199. if (mask_type != 0 || type != mem->type)
  200. return -EINVAL;
  201. if (mem->page_count == 0)
  202. return 0;
  203. for (i = pg_start; i < (mem->page_count + pg_start); i++)
  204. writel(agp_bridge->scratch_page, agp_bridge->gatt_table+nvidia_private.pg_offset+i);
  205. agp_bridge->driver->tlb_flush(mem);
  206. return 0;
  207. }
  208. static void nvidia_tlbflush(struct agp_memory *mem)
  209. {
  210. unsigned long end;
  211. u32 wbc_reg, temp;
  212. int i;
  213. /* flush chipset */
  214. if (nvidia_private.wbc_mask) {
  215. pci_read_config_dword(nvidia_private.dev_1, NVIDIA_1_WBC, &wbc_reg);
  216. wbc_reg |= nvidia_private.wbc_mask;
  217. pci_write_config_dword(nvidia_private.dev_1, NVIDIA_1_WBC, wbc_reg);
  218. end = jiffies + 3*HZ;
  219. do {
  220. pci_read_config_dword(nvidia_private.dev_1,
  221. NVIDIA_1_WBC, &wbc_reg);
  222. if (time_before_eq(end, jiffies)) {
  223. printk(KERN_ERR PFX
  224. "TLB flush took more than 3 seconds.\n");
  225. }
  226. } while (wbc_reg & nvidia_private.wbc_mask);
  227. }
  228. /* flush TLB entries */
  229. for (i = 0; i < 32 + 1; i++)
  230. temp = readl(nvidia_private.aperture+(i * PAGE_SIZE / sizeof(u32)));
  231. for (i = 0; i < 32 + 1; i++)
  232. temp = readl(nvidia_private.aperture+(i * PAGE_SIZE / sizeof(u32)));
  233. }
  234. static const struct aper_size_info_8 nvidia_generic_sizes[5] =
  235. {
  236. {512, 131072, 7, 0},
  237. {256, 65536, 6, 8},
  238. {128, 32768, 5, 12},
  239. {64, 16384, 4, 14},
  240. /* The 32M mode still requires a 64k gatt */
  241. {32, 16384, 4, 15}
  242. };
  243. static const struct gatt_mask nvidia_generic_masks[] =
  244. {
  245. { .mask = 1, .type = 0}
  246. };
  247. static const struct agp_bridge_driver nvidia_driver = {
  248. .owner = THIS_MODULE,
  249. .aperture_sizes = nvidia_generic_sizes,
  250. .size_type = U8_APER_SIZE,
  251. .num_aperture_sizes = 5,
  252. .needs_scratch_page = true,
  253. .configure = nvidia_configure,
  254. .fetch_size = nvidia_fetch_size,
  255. .cleanup = nvidia_cleanup,
  256. .tlb_flush = nvidia_tlbflush,
  257. .mask_memory = agp_generic_mask_memory,
  258. .masks = nvidia_generic_masks,
  259. .agp_enable = agp_generic_enable,
  260. .cache_flush = global_cache_flush,
  261. .create_gatt_table = agp_generic_create_gatt_table,
  262. .free_gatt_table = agp_generic_free_gatt_table,
  263. .insert_memory = nvidia_insert_memory,
  264. .remove_memory = nvidia_remove_memory,
  265. .alloc_by_type = agp_generic_alloc_by_type,
  266. .free_by_type = agp_generic_free_by_type,
  267. .agp_alloc_page = agp_generic_alloc_page,
  268. .agp_alloc_pages = agp_generic_alloc_pages,
  269. .agp_destroy_page = agp_generic_destroy_page,
  270. .agp_destroy_pages = agp_generic_destroy_pages,
  271. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  272. };
  273. static int agp_nvidia_probe(struct pci_dev *pdev,
  274. const struct pci_device_id *ent)
  275. {
  276. struct agp_bridge_data *bridge;
  277. u8 cap_ptr;
  278. nvidia_private.dev_1 =
  279. pci_get_bus_and_slot((unsigned int)pdev->bus->number, PCI_DEVFN(0, 1));
  280. nvidia_private.dev_2 =
  281. pci_get_bus_and_slot((unsigned int)pdev->bus->number, PCI_DEVFN(0, 2));
  282. nvidia_private.dev_3 =
  283. pci_get_bus_and_slot((unsigned int)pdev->bus->number, PCI_DEVFN(30, 0));
  284. if (!nvidia_private.dev_1 || !nvidia_private.dev_2 || !nvidia_private.dev_3) {
  285. printk(KERN_INFO PFX "Detected an NVIDIA nForce/nForce2 "
  286. "chipset, but could not find the secondary devices.\n");
  287. return -ENODEV;
  288. }
  289. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  290. if (!cap_ptr)
  291. return -ENODEV;
  292. switch (pdev->device) {
  293. case PCI_DEVICE_ID_NVIDIA_NFORCE:
  294. printk(KERN_INFO PFX "Detected NVIDIA nForce chipset\n");
  295. nvidia_private.wbc_mask = 0x00010000;
  296. break;
  297. case PCI_DEVICE_ID_NVIDIA_NFORCE2:
  298. printk(KERN_INFO PFX "Detected NVIDIA nForce2 chipset\n");
  299. nvidia_private.wbc_mask = 0x80000000;
  300. break;
  301. default:
  302. printk(KERN_ERR PFX "Unsupported NVIDIA chipset (device id: %04x)\n",
  303. pdev->device);
  304. return -ENODEV;
  305. }
  306. bridge = agp_alloc_bridge();
  307. if (!bridge)
  308. return -ENOMEM;
  309. bridge->driver = &nvidia_driver;
  310. bridge->dev_private_data = &nvidia_private,
  311. bridge->dev = pdev;
  312. bridge->capndx = cap_ptr;
  313. /* Fill in the mode register */
  314. pci_read_config_dword(pdev,
  315. bridge->capndx+PCI_AGP_STATUS,
  316. &bridge->mode);
  317. pci_set_drvdata(pdev, bridge);
  318. return agp_add_bridge(bridge);
  319. }
  320. static void agp_nvidia_remove(struct pci_dev *pdev)
  321. {
  322. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  323. agp_remove_bridge(bridge);
  324. agp_put_bridge(bridge);
  325. }
  326. #ifdef CONFIG_PM
  327. static int agp_nvidia_suspend(struct pci_dev *pdev, pm_message_t state)
  328. {
  329. pci_save_state(pdev);
  330. pci_set_power_state(pdev, PCI_D3hot);
  331. return 0;
  332. }
  333. static int agp_nvidia_resume(struct pci_dev *pdev)
  334. {
  335. /* set power state 0 and restore PCI space */
  336. pci_set_power_state(pdev, PCI_D0);
  337. pci_restore_state(pdev);
  338. /* reconfigure AGP hardware again */
  339. nvidia_configure();
  340. return 0;
  341. }
  342. #endif
  343. static struct pci_device_id agp_nvidia_pci_table[] = {
  344. {
  345. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  346. .class_mask = ~0,
  347. .vendor = PCI_VENDOR_ID_NVIDIA,
  348. .device = PCI_DEVICE_ID_NVIDIA_NFORCE,
  349. .subvendor = PCI_ANY_ID,
  350. .subdevice = PCI_ANY_ID,
  351. },
  352. {
  353. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  354. .class_mask = ~0,
  355. .vendor = PCI_VENDOR_ID_NVIDIA,
  356. .device = PCI_DEVICE_ID_NVIDIA_NFORCE2,
  357. .subvendor = PCI_ANY_ID,
  358. .subdevice = PCI_ANY_ID,
  359. },
  360. { }
  361. };
  362. MODULE_DEVICE_TABLE(pci, agp_nvidia_pci_table);
  363. static struct pci_driver agp_nvidia_pci_driver = {
  364. .name = "agpgart-nvidia",
  365. .id_table = agp_nvidia_pci_table,
  366. .probe = agp_nvidia_probe,
  367. .remove = agp_nvidia_remove,
  368. #ifdef CONFIG_PM
  369. .suspend = agp_nvidia_suspend,
  370. .resume = agp_nvidia_resume,
  371. #endif
  372. };
  373. static int __init agp_nvidia_init(void)
  374. {
  375. if (agp_off)
  376. return -EINVAL;
  377. return pci_register_driver(&agp_nvidia_pci_driver);
  378. }
  379. static void __exit agp_nvidia_cleanup(void)
  380. {
  381. pci_unregister_driver(&agp_nvidia_pci_driver);
  382. pci_dev_put(nvidia_private.dev_1);
  383. pci_dev_put(nvidia_private.dev_2);
  384. pci_dev_put(nvidia_private.dev_3);
  385. }
  386. module_init(agp_nvidia_init);
  387. module_exit(agp_nvidia_cleanup);
  388. MODULE_LICENSE("GPL and additional rights");
  389. MODULE_AUTHOR("NVIDIA Corporation");