backend.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * AGPGART driver backend routines.
  3. * Copyright (C) 2004 Silicon Graphics, Inc.
  4. * Copyright (C) 2002-2003 Dave Jones.
  5. * Copyright (C) 1999 Jeff Hartmann.
  6. * Copyright (C) 1999 Precision Insight, Inc.
  7. * Copyright (C) 1999 Xi Graphics, Inc.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included
  17. * in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * JEFF HARTMANN, DAVE JONES, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  25. * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. * TODO:
  28. * - Allocate more than order 0 pages to avoid too much linear map splitting.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/pci.h>
  32. #include <linux/init.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/miscdevice.h>
  35. #include <linux/pm.h>
  36. #include <linux/agp_backend.h>
  37. #include <linux/agpgart.h>
  38. #include <linux/vmalloc.h>
  39. #include <asm/io.h>
  40. #include "agp.h"
  41. /* Due to XFree86 brain-damage, we can't go to 1.0 until they
  42. * fix some real stupidity. It's only by chance we can bump
  43. * past 0.99 at all due to some boolean logic error. */
  44. #define AGPGART_VERSION_MAJOR 0
  45. #define AGPGART_VERSION_MINOR 103
  46. static const struct agp_version agp_current_version =
  47. {
  48. .major = AGPGART_VERSION_MAJOR,
  49. .minor = AGPGART_VERSION_MINOR,
  50. };
  51. struct agp_bridge_data *(*agp_find_bridge)(struct pci_dev *) =
  52. &agp_generic_find_bridge;
  53. struct agp_bridge_data *agp_bridge;
  54. LIST_HEAD(agp_bridges);
  55. EXPORT_SYMBOL(agp_bridge);
  56. EXPORT_SYMBOL(agp_bridges);
  57. EXPORT_SYMBOL(agp_find_bridge);
  58. /**
  59. * agp_backend_acquire - attempt to acquire an agp backend.
  60. *
  61. */
  62. struct agp_bridge_data *agp_backend_acquire(struct pci_dev *pdev)
  63. {
  64. struct agp_bridge_data *bridge;
  65. bridge = agp_find_bridge(pdev);
  66. if (!bridge)
  67. return NULL;
  68. if (atomic_read(&bridge->agp_in_use))
  69. return NULL;
  70. atomic_inc(&bridge->agp_in_use);
  71. return bridge;
  72. }
  73. EXPORT_SYMBOL(agp_backend_acquire);
  74. /**
  75. * agp_backend_release - release the lock on the agp backend.
  76. *
  77. * The caller must insure that the graphics aperture translation table
  78. * is read for use by another entity.
  79. *
  80. * (Ensure that all memory it bound is unbound.)
  81. */
  82. void agp_backend_release(struct agp_bridge_data *bridge)
  83. {
  84. if (bridge)
  85. atomic_dec(&bridge->agp_in_use);
  86. }
  87. EXPORT_SYMBOL(agp_backend_release);
  88. static const struct { int mem, agp; } maxes_table[] = {
  89. {0, 0},
  90. {32, 4},
  91. {64, 28},
  92. {128, 96},
  93. {256, 204},
  94. {512, 440},
  95. {1024, 942},
  96. {2048, 1920},
  97. {4096, 3932}
  98. };
  99. static int agp_find_max(void)
  100. {
  101. long memory, index, result;
  102. #if PAGE_SHIFT < 20
  103. memory = totalram_pages >> (20 - PAGE_SHIFT);
  104. #else
  105. memory = totalram_pages << (PAGE_SHIFT - 20);
  106. #endif
  107. index = 1;
  108. while ((memory > maxes_table[index].mem) && (index < 8))
  109. index++;
  110. result = maxes_table[index - 1].agp +
  111. ( (memory - maxes_table[index - 1].mem) *
  112. (maxes_table[index].agp - maxes_table[index - 1].agp)) /
  113. (maxes_table[index].mem - maxes_table[index - 1].mem);
  114. result = result << (20 - PAGE_SHIFT);
  115. return result;
  116. }
  117. static int agp_backend_initialize(struct agp_bridge_data *bridge)
  118. {
  119. int size_value, rc, got_gatt=0, got_keylist=0;
  120. bridge->max_memory_agp = agp_find_max();
  121. bridge->version = &agp_current_version;
  122. if (bridge->driver->needs_scratch_page) {
  123. struct page *page = bridge->driver->agp_alloc_page(bridge);
  124. if (!page) {
  125. dev_err(&bridge->dev->dev,
  126. "can't get memory for scratch page\n");
  127. return -ENOMEM;
  128. }
  129. bridge->scratch_page_page = page;
  130. if (bridge->driver->agp_map_page) {
  131. if (bridge->driver->agp_map_page(page,
  132. &bridge->scratch_page_dma)) {
  133. dev_err(&bridge->dev->dev,
  134. "unable to dma-map scratch page\n");
  135. rc = -ENOMEM;
  136. goto err_out_nounmap;
  137. }
  138. } else {
  139. bridge->scratch_page_dma = page_to_phys(page);
  140. }
  141. bridge->scratch_page = bridge->driver->mask_memory(bridge,
  142. bridge->scratch_page_dma, 0);
  143. }
  144. size_value = bridge->driver->fetch_size();
  145. if (size_value == 0) {
  146. dev_err(&bridge->dev->dev, "can't determine aperture size\n");
  147. rc = -EINVAL;
  148. goto err_out;
  149. }
  150. if (bridge->driver->create_gatt_table(bridge)) {
  151. dev_err(&bridge->dev->dev,
  152. "can't get memory for graphics translation table\n");
  153. rc = -ENOMEM;
  154. goto err_out;
  155. }
  156. got_gatt = 1;
  157. bridge->key_list = vmalloc(PAGE_SIZE * 4);
  158. if (bridge->key_list == NULL) {
  159. dev_err(&bridge->dev->dev,
  160. "can't allocate memory for key lists\n");
  161. rc = -ENOMEM;
  162. goto err_out;
  163. }
  164. got_keylist = 1;
  165. /* FIXME vmalloc'd memory not guaranteed contiguous */
  166. memset(bridge->key_list, 0, PAGE_SIZE * 4);
  167. if (bridge->driver->configure()) {
  168. dev_err(&bridge->dev->dev, "error configuring host chipset\n");
  169. rc = -EINVAL;
  170. goto err_out;
  171. }
  172. INIT_LIST_HEAD(&bridge->mapped_list);
  173. spin_lock_init(&bridge->mapped_lock);
  174. return 0;
  175. err_out:
  176. if (bridge->driver->needs_scratch_page &&
  177. bridge->driver->agp_unmap_page) {
  178. bridge->driver->agp_unmap_page(bridge->scratch_page_page,
  179. bridge->scratch_page_dma);
  180. }
  181. err_out_nounmap:
  182. if (bridge->driver->needs_scratch_page) {
  183. void *va = page_address(bridge->scratch_page_page);
  184. bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP);
  185. bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE);
  186. }
  187. if (got_gatt)
  188. bridge->driver->free_gatt_table(bridge);
  189. if (got_keylist) {
  190. vfree(bridge->key_list);
  191. bridge->key_list = NULL;
  192. }
  193. return rc;
  194. }
  195. /* cannot be __exit b/c as it could be called from __init code */
  196. static void agp_backend_cleanup(struct agp_bridge_data *bridge)
  197. {
  198. if (bridge->driver->cleanup)
  199. bridge->driver->cleanup();
  200. if (bridge->driver->free_gatt_table)
  201. bridge->driver->free_gatt_table(bridge);
  202. vfree(bridge->key_list);
  203. bridge->key_list = NULL;
  204. if (bridge->driver->agp_destroy_page &&
  205. bridge->driver->needs_scratch_page) {
  206. void *va = page_address(bridge->scratch_page_page);
  207. if (bridge->driver->agp_unmap_page)
  208. bridge->driver->agp_unmap_page(bridge->scratch_page_page,
  209. bridge->scratch_page_dma);
  210. bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP);
  211. bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE);
  212. }
  213. }
  214. /* When we remove the global variable agp_bridge from all drivers
  215. * then agp_alloc_bridge and agp_generic_find_bridge need to be updated
  216. */
  217. struct agp_bridge_data *agp_alloc_bridge(void)
  218. {
  219. struct agp_bridge_data *bridge;
  220. bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
  221. if (!bridge)
  222. return NULL;
  223. atomic_set(&bridge->agp_in_use, 0);
  224. atomic_set(&bridge->current_memory_agp, 0);
  225. if (list_empty(&agp_bridges))
  226. agp_bridge = bridge;
  227. return bridge;
  228. }
  229. EXPORT_SYMBOL(agp_alloc_bridge);
  230. void agp_put_bridge(struct agp_bridge_data *bridge)
  231. {
  232. kfree(bridge);
  233. if (list_empty(&agp_bridges))
  234. agp_bridge = NULL;
  235. }
  236. EXPORT_SYMBOL(agp_put_bridge);
  237. int agp_add_bridge(struct agp_bridge_data *bridge)
  238. {
  239. int error;
  240. if (agp_off) {
  241. error = -ENODEV;
  242. goto err_put_bridge;
  243. }
  244. if (!bridge->dev) {
  245. printk (KERN_DEBUG PFX "Erk, registering with no pci_dev!\n");
  246. error = -EINVAL;
  247. goto err_put_bridge;
  248. }
  249. /* Grab reference on the chipset driver. */
  250. if (!try_module_get(bridge->driver->owner)) {
  251. dev_info(&bridge->dev->dev, "can't lock chipset driver\n");
  252. error = -EINVAL;
  253. goto err_put_bridge;
  254. }
  255. error = agp_backend_initialize(bridge);
  256. if (error) {
  257. dev_info(&bridge->dev->dev,
  258. "agp_backend_initialize() failed\n");
  259. goto err_out;
  260. }
  261. if (list_empty(&agp_bridges)) {
  262. error = agp_frontend_initialize();
  263. if (error) {
  264. dev_info(&bridge->dev->dev,
  265. "agp_frontend_initialize() failed\n");
  266. goto frontend_err;
  267. }
  268. dev_info(&bridge->dev->dev, "AGP aperture is %dM @ 0x%lx\n",
  269. bridge->driver->fetch_size(), bridge->gart_bus_addr);
  270. }
  271. list_add(&bridge->list, &agp_bridges);
  272. return 0;
  273. frontend_err:
  274. agp_backend_cleanup(bridge);
  275. err_out:
  276. module_put(bridge->driver->owner);
  277. err_put_bridge:
  278. agp_put_bridge(bridge);
  279. return error;
  280. }
  281. EXPORT_SYMBOL_GPL(agp_add_bridge);
  282. void agp_remove_bridge(struct agp_bridge_data *bridge)
  283. {
  284. agp_backend_cleanup(bridge);
  285. list_del(&bridge->list);
  286. if (list_empty(&agp_bridges))
  287. agp_frontend_cleanup();
  288. module_put(bridge->driver->owner);
  289. }
  290. EXPORT_SYMBOL_GPL(agp_remove_bridge);
  291. int agp_off;
  292. int agp_try_unsupported_boot;
  293. EXPORT_SYMBOL(agp_off);
  294. EXPORT_SYMBOL(agp_try_unsupported_boot);
  295. static int __init agp_init(void)
  296. {
  297. if (!agp_off)
  298. printk(KERN_INFO "Linux agpgart interface v%d.%d\n",
  299. AGPGART_VERSION_MAJOR, AGPGART_VERSION_MINOR);
  300. return 0;
  301. }
  302. static void __exit agp_exit(void)
  303. {
  304. }
  305. #ifndef MODULE
  306. static __init int agp_setup(char *s)
  307. {
  308. if (!strcmp(s,"off"))
  309. agp_off = 1;
  310. if (!strcmp(s,"try_unsupported"))
  311. agp_try_unsupported_boot = 1;
  312. return 1;
  313. }
  314. __setup("agp=", agp_setup);
  315. #endif
  316. MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
  317. MODULE_DESCRIPTION("AGP GART driver");
  318. MODULE_LICENSE("GPL and additional rights");
  319. MODULE_ALIAS_MISCDEV(AGPGART_MINOR);
  320. module_init(agp_init);
  321. module_exit(agp_exit);