ati-agp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * ATi 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/string.h>
  9. #include <linux/slab.h>
  10. #include <linux/agp_backend.h>
  11. #include <asm/agp.h>
  12. #include "agp.h"
  13. #define ATI_GART_MMBASE_BAR 1
  14. #define ATI_RS100_APSIZE 0xac
  15. #define ATI_RS100_IG_AGPMODE 0xb0
  16. #define ATI_RS300_APSIZE 0xf8
  17. #define ATI_RS300_IG_AGPMODE 0xfc
  18. #define ATI_GART_FEATURE_ID 0x00
  19. #define ATI_GART_BASE 0x04
  20. #define ATI_GART_CACHE_SZBASE 0x08
  21. #define ATI_GART_CACHE_CNTRL 0x0c
  22. #define ATI_GART_CACHE_ENTRY_CNTRL 0x10
  23. static const struct aper_size_info_lvl2 ati_generic_sizes[7] =
  24. {
  25. {2048, 524288, 0x0000000c},
  26. {1024, 262144, 0x0000000a},
  27. {512, 131072, 0x00000008},
  28. {256, 65536, 0x00000006},
  29. {128, 32768, 0x00000004},
  30. {64, 16384, 0x00000002},
  31. {32, 8192, 0x00000000}
  32. };
  33. static struct gatt_mask ati_generic_masks[] =
  34. {
  35. { .mask = 1, .type = 0}
  36. };
  37. struct ati_page_map {
  38. unsigned long *real;
  39. unsigned long __iomem *remapped;
  40. };
  41. static struct _ati_generic_private {
  42. volatile u8 __iomem *registers;
  43. struct ati_page_map **gatt_pages;
  44. int num_tables;
  45. } ati_generic_private;
  46. static int ati_create_page_map(struct ati_page_map *page_map)
  47. {
  48. int i, err = 0;
  49. page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
  50. if (page_map->real == NULL)
  51. return -ENOMEM;
  52. set_memory_uc((unsigned long)page_map->real, 1);
  53. err = map_page_into_agp(virt_to_page(page_map->real));
  54. page_map->remapped = page_map->real;
  55. for (i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++) {
  56. writel(agp_bridge->scratch_page, page_map->remapped+i);
  57. readl(page_map->remapped+i); /* PCI Posting. */
  58. }
  59. return 0;
  60. }
  61. static void ati_free_page_map(struct ati_page_map *page_map)
  62. {
  63. unmap_page_from_agp(virt_to_page(page_map->real));
  64. set_memory_wb((unsigned long)page_map->real, 1);
  65. free_page((unsigned long) page_map->real);
  66. }
  67. static void ati_free_gatt_pages(void)
  68. {
  69. int i;
  70. struct ati_page_map **tables;
  71. struct ati_page_map *entry;
  72. tables = ati_generic_private.gatt_pages;
  73. for (i = 0; i < ati_generic_private.num_tables; i++) {
  74. entry = tables[i];
  75. if (entry != NULL) {
  76. if (entry->real != NULL)
  77. ati_free_page_map(entry);
  78. kfree(entry);
  79. }
  80. }
  81. kfree(tables);
  82. }
  83. static int ati_create_gatt_pages(int nr_tables)
  84. {
  85. struct ati_page_map **tables;
  86. struct ati_page_map *entry;
  87. int retval = 0;
  88. int i;
  89. tables = kzalloc((nr_tables + 1) * sizeof(struct ati_page_map *),GFP_KERNEL);
  90. if (tables == NULL)
  91. return -ENOMEM;
  92. for (i = 0; i < nr_tables; i++) {
  93. entry = kzalloc(sizeof(struct ati_page_map), GFP_KERNEL);
  94. tables[i] = entry;
  95. if (entry == NULL) {
  96. retval = -ENOMEM;
  97. break;
  98. }
  99. retval = ati_create_page_map(entry);
  100. if (retval != 0)
  101. break;
  102. }
  103. ati_generic_private.num_tables = i;
  104. ati_generic_private.gatt_pages = tables;
  105. if (retval != 0)
  106. ati_free_gatt_pages();
  107. return retval;
  108. }
  109. static int is_r200(void)
  110. {
  111. if ((agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS100) ||
  112. (agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS200) ||
  113. (agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS200_B) ||
  114. (agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS250))
  115. return 1;
  116. return 0;
  117. }
  118. static int ati_fetch_size(void)
  119. {
  120. int i;
  121. u32 temp;
  122. struct aper_size_info_lvl2 *values;
  123. if (is_r200())
  124. pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
  125. else
  126. pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
  127. temp = (temp & 0x0000000e);
  128. values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
  129. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  130. if (temp == values[i].size_value) {
  131. agp_bridge->previous_size =
  132. agp_bridge->current_size = (void *) (values + i);
  133. agp_bridge->aperture_size_idx = i;
  134. return values[i].size;
  135. }
  136. }
  137. return 0;
  138. }
  139. static void ati_tlbflush(struct agp_memory * mem)
  140. {
  141. writel(1, ati_generic_private.registers+ATI_GART_CACHE_CNTRL);
  142. readl(ati_generic_private.registers+ATI_GART_CACHE_CNTRL); /* PCI Posting. */
  143. }
  144. static void ati_cleanup(void)
  145. {
  146. struct aper_size_info_lvl2 *previous_size;
  147. u32 temp;
  148. previous_size = A_SIZE_LVL2(agp_bridge->previous_size);
  149. /* Write back the previous size and disable gart translation */
  150. if (is_r200()) {
  151. pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
  152. temp = ((temp & ~(0x0000000f)) | previous_size->size_value);
  153. pci_write_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, temp);
  154. } else {
  155. pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
  156. temp = ((temp & ~(0x0000000f)) | previous_size->size_value);
  157. pci_write_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, temp);
  158. }
  159. iounmap((volatile u8 __iomem *)ati_generic_private.registers);
  160. }
  161. static int ati_configure(void)
  162. {
  163. phys_addr_t reg;
  164. u32 temp;
  165. /* Get the memory mapped registers */
  166. reg = pci_resource_start(agp_bridge->dev, ATI_GART_MMBASE_BAR);
  167. ati_generic_private.registers = (volatile u8 __iomem *) ioremap(reg, 4096);
  168. if (!ati_generic_private.registers)
  169. return -ENOMEM;
  170. if (is_r200())
  171. pci_write_config_dword(agp_bridge->dev, ATI_RS100_IG_AGPMODE, 0x20000);
  172. else
  173. pci_write_config_dword(agp_bridge->dev, ATI_RS300_IG_AGPMODE, 0x20000);
  174. /* address to map to */
  175. /*
  176. agp_bridge.gart_bus_addr = pci_bus_address(agp_bridge.dev,
  177. AGP_APERTURE_BAR);
  178. printk(KERN_INFO PFX "IGP320 gart_bus_addr: %x\n", agp_bridge.gart_bus_addr);
  179. */
  180. writel(0x60000, ati_generic_private.registers+ATI_GART_FEATURE_ID);
  181. readl(ati_generic_private.registers+ATI_GART_FEATURE_ID); /* PCI Posting.*/
  182. /* SIGNALED_SYSTEM_ERROR @ NB_STATUS */
  183. pci_read_config_dword(agp_bridge->dev, PCI_COMMAND, &temp);
  184. pci_write_config_dword(agp_bridge->dev, PCI_COMMAND, temp | (1<<14));
  185. /* Write out the address of the gatt table */
  186. writel(agp_bridge->gatt_bus_addr, ati_generic_private.registers+ATI_GART_BASE);
  187. readl(ati_generic_private.registers+ATI_GART_BASE); /* PCI Posting. */
  188. return 0;
  189. }
  190. #ifdef CONFIG_PM
  191. static int agp_ati_suspend(struct pci_dev *dev, pm_message_t state)
  192. {
  193. pci_save_state(dev);
  194. pci_set_power_state(dev, PCI_D3hot);
  195. return 0;
  196. }
  197. static int agp_ati_resume(struct pci_dev *dev)
  198. {
  199. pci_set_power_state(dev, PCI_D0);
  200. pci_restore_state(dev);
  201. return ati_configure();
  202. }
  203. #endif
  204. /*
  205. *Since we don't need contiguous memory we just try
  206. * to get the gatt table once
  207. */
  208. #define GET_PAGE_DIR_OFF(addr) (addr >> 22)
  209. #define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
  210. GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
  211. #define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
  212. #undef GET_GATT
  213. #define GET_GATT(addr) (ati_generic_private.gatt_pages[\
  214. GET_PAGE_DIR_IDX(addr)]->remapped)
  215. static int ati_insert_memory(struct agp_memory * mem,
  216. off_t pg_start, int type)
  217. {
  218. int i, j, num_entries;
  219. unsigned long __iomem *cur_gatt;
  220. unsigned long addr;
  221. int mask_type;
  222. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  223. mask_type = agp_generic_type_to_mask_type(mem->bridge, type);
  224. if (mask_type != 0 || type != mem->type)
  225. return -EINVAL;
  226. if (mem->page_count == 0)
  227. return 0;
  228. if ((pg_start + mem->page_count) > num_entries)
  229. return -EINVAL;
  230. j = pg_start;
  231. while (j < (pg_start + mem->page_count)) {
  232. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  233. cur_gatt = GET_GATT(addr);
  234. if (!PGE_EMPTY(agp_bridge,readl(cur_gatt+GET_GATT_OFF(addr))))
  235. return -EBUSY;
  236. j++;
  237. }
  238. if (!mem->is_flushed) {
  239. /*CACHE_FLUSH(); */
  240. global_cache_flush();
  241. mem->is_flushed = true;
  242. }
  243. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  244. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  245. cur_gatt = GET_GATT(addr);
  246. writel(agp_bridge->driver->mask_memory(agp_bridge,
  247. page_to_phys(mem->pages[i]),
  248. mem->type),
  249. cur_gatt+GET_GATT_OFF(addr));
  250. }
  251. readl(GET_GATT(agp_bridge->gart_bus_addr)); /* PCI posting */
  252. agp_bridge->driver->tlb_flush(mem);
  253. return 0;
  254. }
  255. static int ati_remove_memory(struct agp_memory * mem, off_t pg_start,
  256. int type)
  257. {
  258. int i;
  259. unsigned long __iomem *cur_gatt;
  260. unsigned long addr;
  261. int mask_type;
  262. mask_type = agp_generic_type_to_mask_type(mem->bridge, type);
  263. if (mask_type != 0 || type != mem->type)
  264. return -EINVAL;
  265. if (mem->page_count == 0)
  266. return 0;
  267. for (i = pg_start; i < (mem->page_count + pg_start); i++) {
  268. addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  269. cur_gatt = GET_GATT(addr);
  270. writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
  271. }
  272. readl(GET_GATT(agp_bridge->gart_bus_addr)); /* PCI posting */
  273. agp_bridge->driver->tlb_flush(mem);
  274. return 0;
  275. }
  276. static int ati_create_gatt_table(struct agp_bridge_data *bridge)
  277. {
  278. struct aper_size_info_lvl2 *value;
  279. struct ati_page_map page_dir;
  280. unsigned long __iomem *cur_gatt;
  281. unsigned long addr;
  282. int retval;
  283. u32 temp;
  284. int i;
  285. struct aper_size_info_lvl2 *current_size;
  286. value = A_SIZE_LVL2(agp_bridge->current_size);
  287. retval = ati_create_page_map(&page_dir);
  288. if (retval != 0)
  289. return retval;
  290. retval = ati_create_gatt_pages(value->num_entries / 1024);
  291. if (retval != 0) {
  292. ati_free_page_map(&page_dir);
  293. return retval;
  294. }
  295. agp_bridge->gatt_table_real = (u32 *)page_dir.real;
  296. agp_bridge->gatt_table = (u32 __iomem *) page_dir.remapped;
  297. agp_bridge->gatt_bus_addr = virt_to_phys(page_dir.real);
  298. /* Write out the size register */
  299. current_size = A_SIZE_LVL2(agp_bridge->current_size);
  300. if (is_r200()) {
  301. pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
  302. temp = (((temp & ~(0x0000000e)) | current_size->size_value)
  303. | 0x00000001);
  304. pci_write_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, temp);
  305. pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
  306. } else {
  307. pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
  308. temp = (((temp & ~(0x0000000e)) | current_size->size_value)
  309. | 0x00000001);
  310. pci_write_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, temp);
  311. pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
  312. }
  313. /*
  314. * Get the address for the gart region.
  315. * This is a bus address even on the alpha, b/c its
  316. * used to program the agp master not the cpu
  317. */
  318. addr = pci_bus_address(agp_bridge->dev, AGP_APERTURE_BAR);
  319. agp_bridge->gart_bus_addr = addr;
  320. /* Calculate the agp offset */
  321. for (i = 0; i < value->num_entries / 1024; i++, addr += 0x00400000) {
  322. writel(virt_to_phys(ati_generic_private.gatt_pages[i]->real) | 1,
  323. page_dir.remapped+GET_PAGE_DIR_OFF(addr));
  324. readl(page_dir.remapped+GET_PAGE_DIR_OFF(addr)); /* PCI Posting. */
  325. }
  326. for (i = 0; i < value->num_entries; i++) {
  327. addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  328. cur_gatt = GET_GATT(addr);
  329. writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
  330. }
  331. return 0;
  332. }
  333. static int ati_free_gatt_table(struct agp_bridge_data *bridge)
  334. {
  335. struct ati_page_map page_dir;
  336. page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
  337. page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
  338. ati_free_gatt_pages();
  339. ati_free_page_map(&page_dir);
  340. return 0;
  341. }
  342. static const struct agp_bridge_driver ati_generic_bridge = {
  343. .owner = THIS_MODULE,
  344. .aperture_sizes = ati_generic_sizes,
  345. .size_type = LVL2_APER_SIZE,
  346. .num_aperture_sizes = 7,
  347. .needs_scratch_page = true,
  348. .configure = ati_configure,
  349. .fetch_size = ati_fetch_size,
  350. .cleanup = ati_cleanup,
  351. .tlb_flush = ati_tlbflush,
  352. .mask_memory = agp_generic_mask_memory,
  353. .masks = ati_generic_masks,
  354. .agp_enable = agp_generic_enable,
  355. .cache_flush = global_cache_flush,
  356. .create_gatt_table = ati_create_gatt_table,
  357. .free_gatt_table = ati_free_gatt_table,
  358. .insert_memory = ati_insert_memory,
  359. .remove_memory = ati_remove_memory,
  360. .alloc_by_type = agp_generic_alloc_by_type,
  361. .free_by_type = agp_generic_free_by_type,
  362. .agp_alloc_page = agp_generic_alloc_page,
  363. .agp_alloc_pages = agp_generic_alloc_pages,
  364. .agp_destroy_page = agp_generic_destroy_page,
  365. .agp_destroy_pages = agp_generic_destroy_pages,
  366. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  367. };
  368. static struct agp_device_ids ati_agp_device_ids[] =
  369. {
  370. {
  371. .device_id = PCI_DEVICE_ID_ATI_RS100,
  372. .chipset_name = "IGP320/M",
  373. },
  374. {
  375. .device_id = PCI_DEVICE_ID_ATI_RS200,
  376. .chipset_name = "IGP330/340/345/350/M",
  377. },
  378. {
  379. .device_id = PCI_DEVICE_ID_ATI_RS200_B,
  380. .chipset_name = "IGP345M",
  381. },
  382. {
  383. .device_id = PCI_DEVICE_ID_ATI_RS250,
  384. .chipset_name = "IGP7000/M",
  385. },
  386. {
  387. .device_id = PCI_DEVICE_ID_ATI_RS300_100,
  388. .chipset_name = "IGP9100/M",
  389. },
  390. {
  391. .device_id = PCI_DEVICE_ID_ATI_RS300_133,
  392. .chipset_name = "IGP9100/M",
  393. },
  394. {
  395. .device_id = PCI_DEVICE_ID_ATI_RS300_166,
  396. .chipset_name = "IGP9100/M",
  397. },
  398. {
  399. .device_id = PCI_DEVICE_ID_ATI_RS300_200,
  400. .chipset_name = "IGP9100/M",
  401. },
  402. {
  403. .device_id = PCI_DEVICE_ID_ATI_RS350_133,
  404. .chipset_name = "IGP9000/M",
  405. },
  406. {
  407. .device_id = PCI_DEVICE_ID_ATI_RS350_200,
  408. .chipset_name = "IGP9100/M",
  409. },
  410. { }, /* dummy final entry, always present */
  411. };
  412. static int agp_ati_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  413. {
  414. struct agp_device_ids *devs = ati_agp_device_ids;
  415. struct agp_bridge_data *bridge;
  416. u8 cap_ptr;
  417. int j;
  418. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  419. if (!cap_ptr)
  420. return -ENODEV;
  421. /* probe for known chipsets */
  422. for (j = 0; devs[j].chipset_name; j++) {
  423. if (pdev->device == devs[j].device_id)
  424. goto found;
  425. }
  426. dev_err(&pdev->dev, "unsupported Ati chipset [%04x/%04x])\n",
  427. pdev->vendor, pdev->device);
  428. return -ENODEV;
  429. found:
  430. bridge = agp_alloc_bridge();
  431. if (!bridge)
  432. return -ENOMEM;
  433. bridge->dev = pdev;
  434. bridge->capndx = cap_ptr;
  435. bridge->driver = &ati_generic_bridge;
  436. dev_info(&pdev->dev, "Ati %s chipset\n", devs[j].chipset_name);
  437. /* Fill in the mode register */
  438. pci_read_config_dword(pdev,
  439. bridge->capndx+PCI_AGP_STATUS,
  440. &bridge->mode);
  441. pci_set_drvdata(pdev, bridge);
  442. return agp_add_bridge(bridge);
  443. }
  444. static void agp_ati_remove(struct pci_dev *pdev)
  445. {
  446. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  447. agp_remove_bridge(bridge);
  448. agp_put_bridge(bridge);
  449. }
  450. static struct pci_device_id agp_ati_pci_table[] = {
  451. {
  452. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  453. .class_mask = ~0,
  454. .vendor = PCI_VENDOR_ID_ATI,
  455. .device = PCI_ANY_ID,
  456. .subvendor = PCI_ANY_ID,
  457. .subdevice = PCI_ANY_ID,
  458. },
  459. { }
  460. };
  461. MODULE_DEVICE_TABLE(pci, agp_ati_pci_table);
  462. static struct pci_driver agp_ati_pci_driver = {
  463. .name = "agpgart-ati",
  464. .id_table = agp_ati_pci_table,
  465. .probe = agp_ati_probe,
  466. .remove = agp_ati_remove,
  467. #ifdef CONFIG_PM
  468. .suspend = agp_ati_suspend,
  469. .resume = agp_ati_resume,
  470. #endif
  471. };
  472. static int __init agp_ati_init(void)
  473. {
  474. if (agp_off)
  475. return -EINVAL;
  476. return pci_register_driver(&agp_ati_pci_driver);
  477. }
  478. static void __exit agp_ati_cleanup(void)
  479. {
  480. pci_unregister_driver(&agp_ati_pci_driver);
  481. }
  482. module_init(agp_ati_init);
  483. module_exit(agp_ati_cleanup);
  484. MODULE_AUTHOR("Dave Jones");
  485. MODULE_LICENSE("GPL and additional rights");