sgi-agp.c 7.7 KB

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