amd-k7-agp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * AMD K7 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/gfp.h>
  9. #include <linux/page-flags.h>
  10. #include <linux/mm.h>
  11. #include "agp.h"
  12. #define AMD_MMBASE 0x14
  13. #define AMD_APSIZE 0xac
  14. #define AMD_MODECNTL 0xb0
  15. #define AMD_MODECNTL2 0xb2
  16. #define AMD_GARTENABLE 0x02 /* In mmio region (16-bit register) */
  17. #define AMD_ATTBASE 0x04 /* In mmio region (32-bit register) */
  18. #define AMD_TLBFLUSH 0x0c /* In mmio region (32-bit register) */
  19. #define AMD_CACHEENTRY 0x10 /* In mmio region (32-bit register) */
  20. static struct pci_device_id agp_amdk7_pci_table[];
  21. struct amd_page_map {
  22. unsigned long *real;
  23. unsigned long __iomem *remapped;
  24. };
  25. static struct _amd_irongate_private {
  26. volatile u8 __iomem *registers;
  27. struct amd_page_map **gatt_pages;
  28. int num_tables;
  29. } amd_irongate_private;
  30. static int amd_create_page_map(struct amd_page_map *page_map)
  31. {
  32. int i;
  33. page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
  34. if (page_map->real == NULL)
  35. return -ENOMEM;
  36. SetPageReserved(virt_to_page(page_map->real));
  37. global_cache_flush();
  38. page_map->remapped = ioremap_nocache(virt_to_gart(page_map->real),
  39. PAGE_SIZE);
  40. if (page_map->remapped == NULL) {
  41. ClearPageReserved(virt_to_page(page_map->real));
  42. free_page((unsigned long) page_map->real);
  43. page_map->real = NULL;
  44. return -ENOMEM;
  45. }
  46. global_cache_flush();
  47. for (i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++) {
  48. writel(agp_bridge->scratch_page, page_map->remapped+i);
  49. readl(page_map->remapped+i); /* PCI Posting. */
  50. }
  51. return 0;
  52. }
  53. static void amd_free_page_map(struct amd_page_map *page_map)
  54. {
  55. iounmap(page_map->remapped);
  56. ClearPageReserved(virt_to_page(page_map->real));
  57. free_page((unsigned long) page_map->real);
  58. }
  59. static void amd_free_gatt_pages(void)
  60. {
  61. int i;
  62. struct amd_page_map **tables;
  63. struct amd_page_map *entry;
  64. tables = amd_irongate_private.gatt_pages;
  65. for (i = 0; i < amd_irongate_private.num_tables; i++) {
  66. entry = tables[i];
  67. if (entry != NULL) {
  68. if (entry->real != NULL)
  69. amd_free_page_map(entry);
  70. kfree(entry);
  71. }
  72. }
  73. kfree(tables);
  74. amd_irongate_private.gatt_pages = NULL;
  75. }
  76. static int amd_create_gatt_pages(int nr_tables)
  77. {
  78. struct amd_page_map **tables;
  79. struct amd_page_map *entry;
  80. int retval = 0;
  81. int i;
  82. tables = kzalloc((nr_tables + 1) * sizeof(struct amd_page_map *),GFP_KERNEL);
  83. if (tables == NULL)
  84. return -ENOMEM;
  85. for (i = 0; i < nr_tables; i++) {
  86. entry = kzalloc(sizeof(struct amd_page_map), GFP_KERNEL);
  87. if (entry == NULL) {
  88. while (i > 0) {
  89. kfree(tables[i-1]);
  90. i--;
  91. }
  92. kfree(tables);
  93. retval = -ENOMEM;
  94. break;
  95. }
  96. tables[i] = entry;
  97. retval = amd_create_page_map(entry);
  98. if (retval != 0)
  99. break;
  100. }
  101. amd_irongate_private.num_tables = nr_tables;
  102. amd_irongate_private.gatt_pages = tables;
  103. if (retval != 0)
  104. amd_free_gatt_pages();
  105. return retval;
  106. }
  107. /* Since we don't need contiguous memory we just try
  108. * to get the gatt table once
  109. */
  110. #define GET_PAGE_DIR_OFF(addr) (addr >> 22)
  111. #define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
  112. GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
  113. #define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
  114. #define GET_GATT(addr) (amd_irongate_private.gatt_pages[\
  115. GET_PAGE_DIR_IDX(addr)]->remapped)
  116. static int amd_create_gatt_table(struct agp_bridge_data *bridge)
  117. {
  118. struct aper_size_info_lvl2 *value;
  119. struct amd_page_map page_dir;
  120. unsigned long addr;
  121. int retval;
  122. u32 temp;
  123. int i;
  124. value = A_SIZE_LVL2(agp_bridge->current_size);
  125. retval = amd_create_page_map(&page_dir);
  126. if (retval != 0)
  127. return retval;
  128. retval = amd_create_gatt_pages(value->num_entries / 1024);
  129. if (retval != 0) {
  130. amd_free_page_map(&page_dir);
  131. return retval;
  132. }
  133. agp_bridge->gatt_table_real = (u32 *)page_dir.real;
  134. agp_bridge->gatt_table = (u32 __iomem *)page_dir.remapped;
  135. agp_bridge->gatt_bus_addr = virt_to_gart(page_dir.real);
  136. /* Get the address for the gart region.
  137. * This is a bus address even on the alpha, b/c its
  138. * used to program the agp master not the cpu
  139. */
  140. pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
  141. addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  142. agp_bridge->gart_bus_addr = addr;
  143. /* Calculate the agp offset */
  144. for (i = 0; i < value->num_entries / 1024; i++, addr += 0x00400000) {
  145. writel(virt_to_gart(amd_irongate_private.gatt_pages[i]->real) | 1,
  146. page_dir.remapped+GET_PAGE_DIR_OFF(addr));
  147. readl(page_dir.remapped+GET_PAGE_DIR_OFF(addr)); /* PCI Posting. */
  148. }
  149. return 0;
  150. }
  151. static int amd_free_gatt_table(struct agp_bridge_data *bridge)
  152. {
  153. struct amd_page_map page_dir;
  154. page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
  155. page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
  156. amd_free_gatt_pages();
  157. amd_free_page_map(&page_dir);
  158. return 0;
  159. }
  160. static int amd_irongate_fetch_size(void)
  161. {
  162. int i;
  163. u32 temp;
  164. struct aper_size_info_lvl2 *values;
  165. pci_read_config_dword(agp_bridge->dev, AMD_APSIZE, &temp);
  166. temp = (temp & 0x0000000e);
  167. values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
  168. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  169. if (temp == values[i].size_value) {
  170. agp_bridge->previous_size =
  171. agp_bridge->current_size = (void *) (values + i);
  172. agp_bridge->aperture_size_idx = i;
  173. return values[i].size;
  174. }
  175. }
  176. return 0;
  177. }
  178. static int amd_irongate_configure(void)
  179. {
  180. struct aper_size_info_lvl2 *current_size;
  181. u32 temp;
  182. u16 enable_reg;
  183. current_size = A_SIZE_LVL2(agp_bridge->current_size);
  184. /* Get the memory mapped registers */
  185. pci_read_config_dword(agp_bridge->dev, AMD_MMBASE, &temp);
  186. temp = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  187. amd_irongate_private.registers = (volatile u8 __iomem *) ioremap(temp, 4096);
  188. if (!amd_irongate_private.registers)
  189. return -ENOMEM;
  190. /* Write out the address of the gatt table */
  191. writel(agp_bridge->gatt_bus_addr, amd_irongate_private.registers+AMD_ATTBASE);
  192. readl(amd_irongate_private.registers+AMD_ATTBASE); /* PCI Posting. */
  193. /* Write the Sync register */
  194. pci_write_config_byte(agp_bridge->dev, AMD_MODECNTL, 0x80);
  195. /* Set indexing mode */
  196. pci_write_config_byte(agp_bridge->dev, AMD_MODECNTL2, 0x00);
  197. /* Write the enable register */
  198. enable_reg = readw(amd_irongate_private.registers+AMD_GARTENABLE);
  199. enable_reg = (enable_reg | 0x0004);
  200. writew(enable_reg, amd_irongate_private.registers+AMD_GARTENABLE);
  201. readw(amd_irongate_private.registers+AMD_GARTENABLE); /* PCI Posting. */
  202. /* Write out the size register */
  203. pci_read_config_dword(agp_bridge->dev, AMD_APSIZE, &temp);
  204. temp = (((temp & ~(0x0000000e)) | current_size->size_value) | 1);
  205. pci_write_config_dword(agp_bridge->dev, AMD_APSIZE, temp);
  206. /* Flush the tlb */
  207. writel(1, amd_irongate_private.registers+AMD_TLBFLUSH);
  208. readl(amd_irongate_private.registers+AMD_TLBFLUSH); /* PCI Posting.*/
  209. return 0;
  210. }
  211. static void amd_irongate_cleanup(void)
  212. {
  213. struct aper_size_info_lvl2 *previous_size;
  214. u32 temp;
  215. u16 enable_reg;
  216. previous_size = A_SIZE_LVL2(agp_bridge->previous_size);
  217. enable_reg = readw(amd_irongate_private.registers+AMD_GARTENABLE);
  218. enable_reg = (enable_reg & ~(0x0004));
  219. writew(enable_reg, amd_irongate_private.registers+AMD_GARTENABLE);
  220. readw(amd_irongate_private.registers+AMD_GARTENABLE); /* PCI Posting. */
  221. /* Write back the previous size and disable gart translation */
  222. pci_read_config_dword(agp_bridge->dev, AMD_APSIZE, &temp);
  223. temp = ((temp & ~(0x0000000f)) | previous_size->size_value);
  224. pci_write_config_dword(agp_bridge->dev, AMD_APSIZE, temp);
  225. iounmap((void __iomem *) amd_irongate_private.registers);
  226. }
  227. /*
  228. * This routine could be implemented by taking the addresses
  229. * written to the GATT, and flushing them individually. However
  230. * currently it just flushes the whole table. Which is probably
  231. * more efficent, since agp_memory blocks can be a large number of
  232. * entries.
  233. */
  234. static void amd_irongate_tlbflush(struct agp_memory *temp)
  235. {
  236. writel(1, amd_irongate_private.registers+AMD_TLBFLUSH);
  237. readl(amd_irongate_private.registers+AMD_TLBFLUSH); /* PCI Posting. */
  238. }
  239. static int amd_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
  240. {
  241. int i, j, num_entries;
  242. unsigned long __iomem *cur_gatt;
  243. unsigned long addr;
  244. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  245. if (type != 0 || mem->type != 0)
  246. return -EINVAL;
  247. if ((pg_start + mem->page_count) > num_entries)
  248. return -EINVAL;
  249. j = pg_start;
  250. while (j < (pg_start + mem->page_count)) {
  251. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  252. cur_gatt = GET_GATT(addr);
  253. if (!PGE_EMPTY(agp_bridge, readl(cur_gatt+GET_GATT_OFF(addr))))
  254. return -EBUSY;
  255. j++;
  256. }
  257. if (mem->is_flushed == FALSE) {
  258. global_cache_flush();
  259. mem->is_flushed = TRUE;
  260. }
  261. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  262. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  263. cur_gatt = GET_GATT(addr);
  264. writel(agp_generic_mask_memory(agp_bridge,
  265. mem->memory[i], mem->type), cur_gatt+GET_GATT_OFF(addr));
  266. readl(cur_gatt+GET_GATT_OFF(addr)); /* PCI Posting. */
  267. }
  268. amd_irongate_tlbflush(mem);
  269. return 0;
  270. }
  271. static int amd_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
  272. {
  273. int i;
  274. unsigned long __iomem *cur_gatt;
  275. unsigned long addr;
  276. if (type != 0 || mem->type != 0)
  277. return -EINVAL;
  278. for (i = pg_start; i < (mem->page_count + pg_start); i++) {
  279. addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  280. cur_gatt = GET_GATT(addr);
  281. writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
  282. readl(cur_gatt+GET_GATT_OFF(addr)); /* PCI Posting. */
  283. }
  284. amd_irongate_tlbflush(mem);
  285. return 0;
  286. }
  287. static const struct aper_size_info_lvl2 amd_irongate_sizes[7] =
  288. {
  289. {2048, 524288, 0x0000000c},
  290. {1024, 262144, 0x0000000a},
  291. {512, 131072, 0x00000008},
  292. {256, 65536, 0x00000006},
  293. {128, 32768, 0x00000004},
  294. {64, 16384, 0x00000002},
  295. {32, 8192, 0x00000000}
  296. };
  297. static const struct gatt_mask amd_irongate_masks[] =
  298. {
  299. {.mask = 1, .type = 0}
  300. };
  301. static const struct agp_bridge_driver amd_irongate_driver = {
  302. .owner = THIS_MODULE,
  303. .aperture_sizes = amd_irongate_sizes,
  304. .size_type = LVL2_APER_SIZE,
  305. .num_aperture_sizes = 7,
  306. .configure = amd_irongate_configure,
  307. .fetch_size = amd_irongate_fetch_size,
  308. .cleanup = amd_irongate_cleanup,
  309. .tlb_flush = amd_irongate_tlbflush,
  310. .mask_memory = agp_generic_mask_memory,
  311. .masks = amd_irongate_masks,
  312. .agp_enable = agp_generic_enable,
  313. .cache_flush = global_cache_flush,
  314. .create_gatt_table = amd_create_gatt_table,
  315. .free_gatt_table = amd_free_gatt_table,
  316. .insert_memory = amd_insert_memory,
  317. .remove_memory = amd_remove_memory,
  318. .alloc_by_type = agp_generic_alloc_by_type,
  319. .free_by_type = agp_generic_free_by_type,
  320. .agp_alloc_page = agp_generic_alloc_page,
  321. .agp_destroy_page = agp_generic_destroy_page,
  322. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  323. };
  324. static struct agp_device_ids amd_agp_device_ids[] __devinitdata =
  325. {
  326. {
  327. .device_id = PCI_DEVICE_ID_AMD_FE_GATE_7006,
  328. .chipset_name = "Irongate",
  329. },
  330. {
  331. .device_id = PCI_DEVICE_ID_AMD_FE_GATE_700E,
  332. .chipset_name = "761",
  333. },
  334. {
  335. .device_id = PCI_DEVICE_ID_AMD_FE_GATE_700C,
  336. .chipset_name = "760MP",
  337. },
  338. { }, /* dummy final entry, always present */
  339. };
  340. static int __devinit agp_amdk7_probe(struct pci_dev *pdev,
  341. const struct pci_device_id *ent)
  342. {
  343. struct agp_bridge_data *bridge;
  344. u8 cap_ptr;
  345. int j;
  346. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  347. if (!cap_ptr)
  348. return -ENODEV;
  349. j = ent - agp_amdk7_pci_table;
  350. printk(KERN_INFO PFX "Detected AMD %s chipset\n",
  351. amd_agp_device_ids[j].chipset_name);
  352. bridge = agp_alloc_bridge();
  353. if (!bridge)
  354. return -ENOMEM;
  355. bridge->driver = &amd_irongate_driver;
  356. bridge->dev_private_data = &amd_irongate_private,
  357. bridge->dev = pdev;
  358. bridge->capndx = cap_ptr;
  359. /* 751 Errata (22564_B-1.PDF)
  360. erratum 20: strobe glitch with Nvidia NV10 GeForce cards.
  361. system controller may experience noise due to strong drive strengths
  362. */
  363. if (agp_bridge->dev->device == PCI_DEVICE_ID_AMD_FE_GATE_7006) {
  364. u8 cap_ptr=0;
  365. struct pci_dev *gfxcard=NULL;
  366. while (!cap_ptr) {
  367. gfxcard = pci_get_class(PCI_CLASS_DISPLAY_VGA<<8, gfxcard);
  368. if (!gfxcard) {
  369. printk (KERN_INFO PFX "Couldn't find an AGP VGA controller.\n");
  370. return -ENODEV;
  371. }
  372. cap_ptr = pci_find_capability(gfxcard, PCI_CAP_ID_AGP);
  373. if (!cap_ptr) {
  374. pci_dev_put(gfxcard);
  375. continue;
  376. }
  377. }
  378. /* With so many variants of NVidia cards, it's simpler just
  379. to blacklist them all, and then whitelist them as needed
  380. (if necessary at all). */
  381. if (gfxcard->vendor == PCI_VENDOR_ID_NVIDIA) {
  382. agp_bridge->flags |= AGP_ERRATA_1X;
  383. printk (KERN_INFO PFX "AMD 751 chipset with NVidia GeForce detected. Forcing to 1X due to errata.\n");
  384. }
  385. pci_dev_put(gfxcard);
  386. }
  387. /* 761 Errata (23613_F.pdf)
  388. * Revisions B0/B1 were a disaster.
  389. * erratum 44: SYSCLK/AGPCLK skew causes 2X failures -- Force mode to 1X
  390. * erratum 45: Timing problem prevents fast writes -- Disable fast write.
  391. * erratum 46: Setup violation on AGP SBA pins - Disable side band addressing.
  392. * With this lot disabled, we should prevent lockups. */
  393. if (agp_bridge->dev->device == PCI_DEVICE_ID_AMD_FE_GATE_700E) {
  394. if (pdev->revision == 0x10 || pdev->revision == 0x11) {
  395. agp_bridge->flags = AGP_ERRATA_FASTWRITES;
  396. agp_bridge->flags |= AGP_ERRATA_SBA;
  397. agp_bridge->flags |= AGP_ERRATA_1X;
  398. printk (KERN_INFO PFX "AMD 761 chipset with errata detected - disabling AGP fast writes & SBA and forcing to 1X.\n");
  399. }
  400. }
  401. /* Fill in the mode register */
  402. pci_read_config_dword(pdev,
  403. bridge->capndx+PCI_AGP_STATUS,
  404. &bridge->mode);
  405. pci_set_drvdata(pdev, bridge);
  406. return agp_add_bridge(bridge);
  407. }
  408. static void __devexit agp_amdk7_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. /* must be the same order as name table above */
  415. static struct pci_device_id agp_amdk7_pci_table[] = {
  416. {
  417. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  418. .class_mask = ~0,
  419. .vendor = PCI_VENDOR_ID_AMD,
  420. .device = PCI_DEVICE_ID_AMD_FE_GATE_7006,
  421. .subvendor = PCI_ANY_ID,
  422. .subdevice = PCI_ANY_ID,
  423. },
  424. {
  425. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  426. .class_mask = ~0,
  427. .vendor = PCI_VENDOR_ID_AMD,
  428. .device = PCI_DEVICE_ID_AMD_FE_GATE_700E,
  429. .subvendor = PCI_ANY_ID,
  430. .subdevice = PCI_ANY_ID,
  431. },
  432. {
  433. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  434. .class_mask = ~0,
  435. .vendor = PCI_VENDOR_ID_AMD,
  436. .device = PCI_DEVICE_ID_AMD_FE_GATE_700C,
  437. .subvendor = PCI_ANY_ID,
  438. .subdevice = PCI_ANY_ID,
  439. },
  440. { }
  441. };
  442. MODULE_DEVICE_TABLE(pci, agp_amdk7_pci_table);
  443. static struct pci_driver agp_amdk7_pci_driver = {
  444. .name = "agpgart-amdk7",
  445. .id_table = agp_amdk7_pci_table,
  446. .probe = agp_amdk7_probe,
  447. .remove = agp_amdk7_remove,
  448. };
  449. static int __init agp_amdk7_init(void)
  450. {
  451. if (agp_off)
  452. return -EINVAL;
  453. return pci_register_driver(&agp_amdk7_pci_driver);
  454. }
  455. static void __exit agp_amdk7_cleanup(void)
  456. {
  457. pci_unregister_driver(&agp_amdk7_pci_driver);
  458. }
  459. module_init(agp_amdk7_init);
  460. module_exit(agp_amdk7_cleanup);
  461. MODULE_LICENSE("GPL and additional rights");