sgi-agp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2003-2005 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * SGI TIOCA AGPGART routines.
  10. *
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/module.h>
  14. #include <linux/pci.h>
  15. #include <linux/init.h>
  16. #include <linux/agp_backend.h>
  17. #include <asm/sn/addrs.h>
  18. #include <asm/sn/pcidev.h>
  19. #include <asm/sn/pcibus_provider_defs.h>
  20. #include <asm/sn/tioca_provider.h>
  21. #include "agp.h"
  22. extern int agp_memory_reserved;
  23. extern uint32_t tioca_gart_found;
  24. extern struct list_head tioca_list;
  25. static struct agp_bridge_data **sgi_tioca_agp_bridges;
  26. /*
  27. * The aperature size and related information is set up at TIOCA init time.
  28. * Values for this table will be extracted and filled in at
  29. * sgi_tioca_fetch_size() time.
  30. */
  31. static struct aper_size_info_fixed sgi_tioca_sizes[] = {
  32. {0, 0, 0},
  33. };
  34. static void *sgi_tioca_alloc_page(struct agp_bridge_data *bridge)
  35. {
  36. struct page *page;
  37. int nid;
  38. struct tioca_kernel *info =
  39. (struct tioca_kernel *)bridge->dev_private_data;
  40. nid = info->ca_closest_node;
  41. page = alloc_pages_node(nid, GFP_KERNEL, 0);
  42. if (page == NULL) {
  43. return 0;
  44. }
  45. get_page(page);
  46. SetPageLocked(page);
  47. atomic_inc(&agp_bridge->current_memory_agp);
  48. return page_address(page);
  49. }
  50. /*
  51. * Flush GART tlb's. Cannot selectively flush based on memory so the mem
  52. * arg is ignored.
  53. */
  54. static void sgi_tioca_tlbflush(struct agp_memory *mem)
  55. {
  56. tioca_tlbflush(mem->bridge->dev_private_data);
  57. }
  58. /*
  59. * Given an address of a host physical page, turn it into a valid gart
  60. * entry.
  61. */
  62. static unsigned long
  63. sgi_tioca_mask_memory(struct agp_bridge_data *bridge,
  64. unsigned long addr, int type)
  65. {
  66. return tioca_physpage_to_gart(addr);
  67. }
  68. static void sgi_tioca_agp_enable(struct agp_bridge_data *bridge, u32 mode)
  69. {
  70. tioca_fastwrite_enable(bridge->dev_private_data);
  71. }
  72. /*
  73. * sgi_tioca_configure() doesn't have anything to do since the base CA driver
  74. * has alreay set up the GART.
  75. */
  76. static int sgi_tioca_configure(void)
  77. {
  78. return 0;
  79. }
  80. /*
  81. * Determine gfx aperature size. This has already been determined by the
  82. * CA driver init, so just need to set agp_bridge values accordingly.
  83. */
  84. static int sgi_tioca_fetch_size(void)
  85. {
  86. struct tioca_kernel *info =
  87. (struct tioca_kernel *)agp_bridge->dev_private_data;
  88. sgi_tioca_sizes[0].size = info->ca_gfxap_size / MB(1);
  89. sgi_tioca_sizes[0].num_entries = info->ca_gfxgart_entries;
  90. return sgi_tioca_sizes[0].size;
  91. }
  92. static int sgi_tioca_create_gatt_table(struct agp_bridge_data *bridge)
  93. {
  94. struct tioca_kernel *info =
  95. (struct tioca_kernel *)bridge->dev_private_data;
  96. bridge->gatt_table_real = (u32 *) info->ca_gfxgart;
  97. bridge->gatt_table = bridge->gatt_table_real;
  98. bridge->gatt_bus_addr = info->ca_gfxgart_base;
  99. return 0;
  100. }
  101. static int sgi_tioca_free_gatt_table(struct agp_bridge_data *bridge)
  102. {
  103. return 0;
  104. }
  105. static int sgi_tioca_insert_memory(struct agp_memory *mem, off_t pg_start,
  106. int type)
  107. {
  108. int num_entries;
  109. size_t i;
  110. off_t j;
  111. void *temp;
  112. struct agp_bridge_data *bridge;
  113. u64 *table;
  114. bridge = mem->bridge;
  115. if (!bridge)
  116. return -EINVAL;
  117. table = (u64 *)bridge->gatt_table;
  118. temp = bridge->current_size;
  119. switch (bridge->driver->size_type) {
  120. case U8_APER_SIZE:
  121. num_entries = A_SIZE_8(temp)->num_entries;
  122. break;
  123. case U16_APER_SIZE:
  124. num_entries = A_SIZE_16(temp)->num_entries;
  125. break;
  126. case U32_APER_SIZE:
  127. num_entries = A_SIZE_32(temp)->num_entries;
  128. break;
  129. case FIXED_APER_SIZE:
  130. num_entries = A_SIZE_FIX(temp)->num_entries;
  131. break;
  132. case LVL2_APER_SIZE:
  133. return -EINVAL;
  134. break;
  135. default:
  136. num_entries = 0;
  137. break;
  138. }
  139. num_entries -= agp_memory_reserved / PAGE_SIZE;
  140. if (num_entries < 0)
  141. num_entries = 0;
  142. if (type != 0 || mem->type != 0) {
  143. return -EINVAL;
  144. }
  145. if ((pg_start + mem->page_count) > num_entries)
  146. return -EINVAL;
  147. j = pg_start;
  148. while (j < (pg_start + mem->page_count)) {
  149. if (table[j])
  150. return -EBUSY;
  151. j++;
  152. }
  153. if (mem->is_flushed == FALSE) {
  154. bridge->driver->cache_flush();
  155. mem->is_flushed = TRUE;
  156. }
  157. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  158. table[j] =
  159. bridge->driver->mask_memory(bridge, mem->memory[i],
  160. mem->type);
  161. }
  162. bridge->driver->tlb_flush(mem);
  163. return 0;
  164. }
  165. static int sgi_tioca_remove_memory(struct agp_memory *mem, off_t pg_start,
  166. int type)
  167. {
  168. size_t i;
  169. struct agp_bridge_data *bridge;
  170. u64 *table;
  171. bridge = mem->bridge;
  172. if (!bridge)
  173. return -EINVAL;
  174. if (type != 0 || mem->type != 0) {
  175. return -EINVAL;
  176. }
  177. table = (u64 *)bridge->gatt_table;
  178. for (i = pg_start; i < (mem->page_count + pg_start); i++) {
  179. table[i] = 0;
  180. }
  181. bridge->driver->tlb_flush(mem);
  182. return 0;
  183. }
  184. static void sgi_tioca_cache_flush(void)
  185. {
  186. }
  187. /*
  188. * Cleanup. Nothing to do as the CA driver owns the GART.
  189. */
  190. static void sgi_tioca_cleanup(void)
  191. {
  192. }
  193. static struct agp_bridge_data *sgi_tioca_find_bridge(struct pci_dev *pdev)
  194. {
  195. struct agp_bridge_data *bridge;
  196. list_for_each_entry(bridge, &agp_bridges, list) {
  197. if (bridge->dev->bus == pdev->bus)
  198. break;
  199. }
  200. return bridge;
  201. }
  202. struct agp_bridge_driver sgi_tioca_driver = {
  203. .owner = THIS_MODULE,
  204. .size_type = U16_APER_SIZE,
  205. .configure = sgi_tioca_configure,
  206. .fetch_size = sgi_tioca_fetch_size,
  207. .cleanup = sgi_tioca_cleanup,
  208. .tlb_flush = sgi_tioca_tlbflush,
  209. .mask_memory = sgi_tioca_mask_memory,
  210. .agp_enable = sgi_tioca_agp_enable,
  211. .cache_flush = sgi_tioca_cache_flush,
  212. .create_gatt_table = sgi_tioca_create_gatt_table,
  213. .free_gatt_table = sgi_tioca_free_gatt_table,
  214. .insert_memory = sgi_tioca_insert_memory,
  215. .remove_memory = sgi_tioca_remove_memory,
  216. .alloc_by_type = agp_generic_alloc_by_type,
  217. .free_by_type = agp_generic_free_by_type,
  218. .agp_alloc_page = sgi_tioca_alloc_page,
  219. .agp_destroy_page = agp_generic_destroy_page,
  220. .cant_use_aperture = 1,
  221. .needs_scratch_page = 0,
  222. .num_aperture_sizes = 1,
  223. };
  224. static int __devinit agp_sgi_init(void)
  225. {
  226. unsigned int j;
  227. struct tioca_kernel *info;
  228. struct pci_dev *pdev = NULL;
  229. if (tioca_gart_found)
  230. printk(KERN_INFO PFX "SGI TIO CA GART driver initialized.\n");
  231. else
  232. return 0;
  233. sgi_tioca_agp_bridges =
  234. (struct agp_bridge_data **)kmalloc(tioca_gart_found *
  235. sizeof(struct agp_bridge_data *),
  236. GFP_KERNEL);
  237. j = 0;
  238. list_for_each_entry(info, &tioca_list, ca_list) {
  239. struct list_head *tmp;
  240. list_for_each(tmp, info->ca_devices) {
  241. u8 cap_ptr;
  242. pdev = pci_dev_b(tmp);
  243. if (pdev->class != (PCI_CLASS_DISPLAY_VGA << 8))
  244. continue;
  245. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  246. if (!cap_ptr)
  247. continue;
  248. }
  249. sgi_tioca_agp_bridges[j] = agp_alloc_bridge();
  250. printk(KERN_INFO PFX "bridge %d = 0x%p\n", j,
  251. sgi_tioca_agp_bridges[j]);
  252. if (sgi_tioca_agp_bridges[j]) {
  253. sgi_tioca_agp_bridges[j]->dev = pdev;
  254. sgi_tioca_agp_bridges[j]->dev_private_data = info;
  255. sgi_tioca_agp_bridges[j]->driver = &sgi_tioca_driver;
  256. sgi_tioca_agp_bridges[j]->gart_bus_addr =
  257. info->ca_gfxap_base;
  258. sgi_tioca_agp_bridges[j]->mode = (0x7D << 24) | /* 126 requests */
  259. (0x1 << 9) | /* SBA supported */
  260. (0x1 << 5) | /* 64-bit addresses supported */
  261. (0x1 << 4) | /* FW supported */
  262. (0x1 << 3) | /* AGP 3.0 mode */
  263. 0x2; /* 8x transfer only */
  264. sgi_tioca_agp_bridges[j]->current_size =
  265. sgi_tioca_agp_bridges[j]->previous_size =
  266. (void *)&sgi_tioca_sizes[0];
  267. agp_add_bridge(sgi_tioca_agp_bridges[j]);
  268. }
  269. j++;
  270. }
  271. agp_find_bridge = &sgi_tioca_find_bridge;
  272. return 0;
  273. }
  274. static void __devexit agp_sgi_cleanup(void)
  275. {
  276. if(sgi_tioca_agp_bridges)
  277. kfree(sgi_tioca_agp_bridges);
  278. sgi_tioca_agp_bridges=NULL;
  279. }
  280. module_init(agp_sgi_init);
  281. module_exit(agp_sgi_cleanup);
  282. MODULE_LICENSE("GPL and additional rights");