sgi-agp.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/agp_backend.h>
  18. #include <asm/sn/addrs.h>
  19. #include <asm/sn/io.h>
  20. #include <asm/sn/pcidev.h>
  21. #include <asm/sn/pcibus_provider_defs.h>
  22. #include <asm/sn/tioca_provider.h>
  23. #include "agp.h"
  24. extern int agp_memory_reserved;
  25. extern uint32_t tioca_gart_found;
  26. extern struct list_head tioca_list;
  27. static struct agp_bridge_data **sgi_tioca_agp_bridges;
  28. /*
  29. * The aperature size and related information is set up at TIOCA init time.
  30. * Values for this table will be extracted and filled in at
  31. * sgi_tioca_fetch_size() time.
  32. */
  33. static struct aper_size_info_fixed sgi_tioca_sizes[] = {
  34. {0, 0, 0},
  35. };
  36. static struct page *sgi_tioca_alloc_page(struct agp_bridge_data *bridge)
  37. {
  38. struct page *page;
  39. int nid;
  40. struct tioca_kernel *info =
  41. (struct tioca_kernel *)bridge->dev_private_data;
  42. nid = info->ca_closest_node;
  43. page = alloc_pages_node(nid, GFP_KERNEL, 0);
  44. if (!page)
  45. return NULL;
  46. get_page(page);
  47. atomic_inc(&agp_bridge->current_memory_agp);
  48. return 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, dma_addr_t addr,
  64. 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) {
  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,
  160. page_to_phys(mem->pages[i]),
  161. mem->type);
  162. }
  163. bridge->driver->tlb_flush(mem);
  164. return 0;
  165. }
  166. static int sgi_tioca_remove_memory(struct agp_memory *mem, off_t pg_start,
  167. int type)
  168. {
  169. size_t i;
  170. struct agp_bridge_data *bridge;
  171. u64 *table;
  172. bridge = mem->bridge;
  173. if (!bridge)
  174. return -EINVAL;
  175. if (type != 0 || mem->type != 0) {
  176. return -EINVAL;
  177. }
  178. table = (u64 *)bridge->gatt_table;
  179. for (i = pg_start; i < (mem->page_count + pg_start); i++) {
  180. table[i] = 0;
  181. }
  182. bridge->driver->tlb_flush(mem);
  183. return 0;
  184. }
  185. static void sgi_tioca_cache_flush(void)
  186. {
  187. }
  188. /*
  189. * Cleanup. Nothing to do as the CA driver owns the GART.
  190. */
  191. static void sgi_tioca_cleanup(void)
  192. {
  193. }
  194. static struct agp_bridge_data *sgi_tioca_find_bridge(struct pci_dev *pdev)
  195. {
  196. struct agp_bridge_data *bridge;
  197. list_for_each_entry(bridge, &agp_bridges, list) {
  198. if (bridge->dev->bus == pdev->bus)
  199. break;
  200. }
  201. return bridge;
  202. }
  203. const struct agp_bridge_driver sgi_tioca_driver = {
  204. .owner = THIS_MODULE,
  205. .size_type = U16_APER_SIZE,
  206. .configure = sgi_tioca_configure,
  207. .fetch_size = sgi_tioca_fetch_size,
  208. .cleanup = sgi_tioca_cleanup,
  209. .tlb_flush = sgi_tioca_tlbflush,
  210. .mask_memory = sgi_tioca_mask_memory,
  211. .agp_enable = sgi_tioca_agp_enable,
  212. .cache_flush = sgi_tioca_cache_flush,
  213. .create_gatt_table = sgi_tioca_create_gatt_table,
  214. .free_gatt_table = sgi_tioca_free_gatt_table,
  215. .insert_memory = sgi_tioca_insert_memory,
  216. .remove_memory = sgi_tioca_remove_memory,
  217. .alloc_by_type = agp_generic_alloc_by_type,
  218. .free_by_type = agp_generic_free_by_type,
  219. .agp_alloc_page = sgi_tioca_alloc_page,
  220. .agp_destroy_page = agp_generic_destroy_page,
  221. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  222. .cant_use_aperture = true,
  223. .needs_scratch_page = false,
  224. .num_aperture_sizes = 1,
  225. };
  226. static int __devinit agp_sgi_init(void)
  227. {
  228. unsigned int j;
  229. struct tioca_kernel *info;
  230. struct pci_dev *pdev = NULL;
  231. if (tioca_gart_found)
  232. printk(KERN_INFO PFX "SGI TIO CA GART driver initialized.\n");
  233. else
  234. return 0;
  235. sgi_tioca_agp_bridges = kmalloc(tioca_gart_found *
  236. sizeof(struct agp_bridge_data *),
  237. GFP_KERNEL);
  238. if (!sgi_tioca_agp_bridges)
  239. return -ENOMEM;
  240. j = 0;
  241. list_for_each_entry(info, &tioca_list, ca_list) {
  242. struct list_head *tmp;
  243. if (list_empty(info->ca_devices))
  244. continue;
  245. list_for_each(tmp, info->ca_devices) {
  246. u8 cap_ptr;
  247. pdev = pci_dev_b(tmp);
  248. if (pdev->class != (PCI_CLASS_DISPLAY_VGA << 8))
  249. continue;
  250. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  251. if (!cap_ptr)
  252. continue;
  253. }
  254. sgi_tioca_agp_bridges[j] = agp_alloc_bridge();
  255. printk(KERN_INFO PFX "bridge %d = 0x%p\n", j,
  256. sgi_tioca_agp_bridges[j]);
  257. if (sgi_tioca_agp_bridges[j]) {
  258. sgi_tioca_agp_bridges[j]->dev = pdev;
  259. sgi_tioca_agp_bridges[j]->dev_private_data = info;
  260. sgi_tioca_agp_bridges[j]->driver = &sgi_tioca_driver;
  261. sgi_tioca_agp_bridges[j]->gart_bus_addr =
  262. info->ca_gfxap_base;
  263. sgi_tioca_agp_bridges[j]->mode = (0x7D << 24) | /* 126 requests */
  264. (0x1 << 9) | /* SBA supported */
  265. (0x1 << 5) | /* 64-bit addresses supported */
  266. (0x1 << 4) | /* FW supported */
  267. (0x1 << 3) | /* AGP 3.0 mode */
  268. 0x2; /* 8x transfer only */
  269. sgi_tioca_agp_bridges[j]->current_size =
  270. sgi_tioca_agp_bridges[j]->previous_size =
  271. (void *)&sgi_tioca_sizes[0];
  272. agp_add_bridge(sgi_tioca_agp_bridges[j]);
  273. }
  274. j++;
  275. }
  276. agp_find_bridge = &sgi_tioca_find_bridge;
  277. return 0;
  278. }
  279. static void __devexit agp_sgi_cleanup(void)
  280. {
  281. kfree(sgi_tioca_agp_bridges);
  282. sgi_tioca_agp_bridges = NULL;
  283. }
  284. module_init(agp_sgi_init);
  285. module_exit(agp_sgi_cleanup);
  286. MODULE_LICENSE("GPL and additional rights");