backend.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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/slab.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/miscdevice.h>
  36. #include <linux/pm.h>
  37. #include <linux/agp_backend.h>
  38. #include <linux/agpgart.h>
  39. #include <linux/vmalloc.h>
  40. #include <asm/io.h>
  41. #include "agp.h"
  42. /* Due to XFree86 brain-damage, we can't go to 1.0 until they
  43. * fix some real stupidity. It's only by chance we can bump
  44. * past 0.99 at all due to some boolean logic error. */
  45. #define AGPGART_VERSION_MAJOR 0
  46. #define AGPGART_VERSION_MINOR 103
  47. static const struct agp_version agp_current_version =
  48. {
  49. .major = AGPGART_VERSION_MAJOR,
  50. .minor = AGPGART_VERSION_MINOR,
  51. };
  52. struct agp_bridge_data *(*agp_find_bridge)(struct pci_dev *) =
  53. &agp_generic_find_bridge;
  54. struct agp_bridge_data *agp_bridge;
  55. LIST_HEAD(agp_bridges);
  56. EXPORT_SYMBOL(agp_bridge);
  57. EXPORT_SYMBOL(agp_bridges);
  58. EXPORT_SYMBOL(agp_find_bridge);
  59. /**
  60. * agp_backend_acquire - attempt to acquire an agp backend.
  61. *
  62. */
  63. struct agp_bridge_data *agp_backend_acquire(struct pci_dev *pdev)
  64. {
  65. struct agp_bridge_data *bridge;
  66. bridge = agp_find_bridge(pdev);
  67. if (!bridge)
  68. return NULL;
  69. if (atomic_read(&bridge->agp_in_use))
  70. return NULL;
  71. atomic_inc(&bridge->agp_in_use);
  72. return bridge;
  73. }
  74. EXPORT_SYMBOL(agp_backend_acquire);
  75. /**
  76. * agp_backend_release - release the lock on the agp backend.
  77. *
  78. * The caller must insure that the graphics aperture translation table
  79. * is read for use by another entity.
  80. *
  81. * (Ensure that all memory it bound is unbound.)
  82. */
  83. void agp_backend_release(struct agp_bridge_data *bridge)
  84. {
  85. if (bridge)
  86. atomic_dec(&bridge->agp_in_use);
  87. }
  88. EXPORT_SYMBOL(agp_backend_release);
  89. static const struct { int mem, agp; } maxes_table[] = {
  90. {0, 0},
  91. {32, 4},
  92. {64, 28},
  93. {128, 96},
  94. {256, 204},
  95. {512, 440},
  96. {1024, 942},
  97. {2048, 1920},
  98. {4096, 3932}
  99. };
  100. static int agp_find_max(void)
  101. {
  102. long memory, index, result;
  103. #if PAGE_SHIFT < 20
  104. memory = totalram_pages >> (20 - PAGE_SHIFT);
  105. #else
  106. memory = totalram_pages << (PAGE_SHIFT - 20);
  107. #endif
  108. index = 1;
  109. while ((memory > maxes_table[index].mem) && (index < 8))
  110. index++;
  111. result = maxes_table[index - 1].agp +
  112. ( (memory - maxes_table[index - 1].mem) *
  113. (maxes_table[index].agp - maxes_table[index - 1].agp)) /
  114. (maxes_table[index].mem - maxes_table[index - 1].mem);
  115. result = result << (20 - PAGE_SHIFT);
  116. return result;
  117. }
  118. static int agp_backend_initialize(struct agp_bridge_data *bridge)
  119. {
  120. int size_value, rc, got_gatt=0, got_keylist=0;
  121. bridge->max_memory_agp = agp_find_max();
  122. bridge->version = &agp_current_version;
  123. if (bridge->driver->needs_scratch_page) {
  124. struct page *page = bridge->driver->agp_alloc_page(bridge);
  125. if (!page) {
  126. dev_err(&bridge->dev->dev,
  127. "can't get memory for scratch page\n");
  128. return -ENOMEM;
  129. }
  130. bridge->scratch_page_page = page;
  131. if (bridge->driver->agp_map_page) {
  132. if (bridge->driver->agp_map_page(page,
  133. &bridge->scratch_page_dma)) {
  134. dev_err(&bridge->dev->dev,
  135. "unable to dma-map scratch page\n");
  136. rc = -ENOMEM;
  137. goto err_out_nounmap;
  138. }
  139. } else {
  140. bridge->scratch_page_dma = page_to_phys(page);
  141. }
  142. bridge->scratch_page = bridge->driver->mask_memory(bridge,
  143. bridge->scratch_page_dma, 0);
  144. }
  145. size_value = bridge->driver->fetch_size();
  146. if (size_value == 0) {
  147. dev_err(&bridge->dev->dev, "can't determine aperture size\n");
  148. rc = -EINVAL;
  149. goto err_out;
  150. }
  151. if (bridge->driver->create_gatt_table(bridge)) {
  152. dev_err(&bridge->dev->dev,
  153. "can't get memory for graphics translation table\n");
  154. rc = -ENOMEM;
  155. goto err_out;
  156. }
  157. got_gatt = 1;
  158. bridge->key_list = vmalloc(PAGE_SIZE * 4);
  159. if (bridge->key_list == NULL) {
  160. dev_err(&bridge->dev->dev,
  161. "can't allocate memory for key lists\n");
  162. rc = -ENOMEM;
  163. goto err_out;
  164. }
  165. got_keylist = 1;
  166. /* FIXME vmalloc'd memory not guaranteed contiguous */
  167. memset(bridge->key_list, 0, PAGE_SIZE * 4);
  168. if (bridge->driver->configure()) {
  169. dev_err(&bridge->dev->dev, "error configuring host chipset\n");
  170. rc = -EINVAL;
  171. goto err_out;
  172. }
  173. INIT_LIST_HEAD(&bridge->mapped_list);
  174. spin_lock_init(&bridge->mapped_lock);
  175. return 0;
  176. err_out:
  177. if (bridge->driver->needs_scratch_page &&
  178. bridge->driver->agp_unmap_page) {
  179. bridge->driver->agp_unmap_page(bridge->scratch_page_page,
  180. bridge->scratch_page_dma);
  181. }
  182. err_out_nounmap:
  183. if (bridge->driver->needs_scratch_page) {
  184. void *va = page_address(bridge->scratch_page_page);
  185. bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP);
  186. bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE);
  187. }
  188. if (got_gatt)
  189. bridge->driver->free_gatt_table(bridge);
  190. if (got_keylist) {
  191. vfree(bridge->key_list);
  192. bridge->key_list = NULL;
  193. }
  194. return rc;
  195. }
  196. /* cannot be __exit b/c as it could be called from __init code */
  197. static void agp_backend_cleanup(struct agp_bridge_data *bridge)
  198. {
  199. if (bridge->driver->cleanup)
  200. bridge->driver->cleanup();
  201. if (bridge->driver->free_gatt_table)
  202. bridge->driver->free_gatt_table(bridge);
  203. vfree(bridge->key_list);
  204. bridge->key_list = NULL;
  205. if (bridge->driver->agp_destroy_page &&
  206. bridge->driver->needs_scratch_page) {
  207. void *va = page_address(bridge->scratch_page_page);
  208. if (bridge->driver->agp_unmap_page)
  209. bridge->driver->agp_unmap_page(bridge->scratch_page_page,
  210. bridge->scratch_page_dma);
  211. bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP);
  212. bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE);
  213. }
  214. }
  215. /* When we remove the global variable agp_bridge from all drivers
  216. * then agp_alloc_bridge and agp_generic_find_bridge need to be updated
  217. */
  218. struct agp_bridge_data *agp_alloc_bridge(void)
  219. {
  220. struct agp_bridge_data *bridge;
  221. bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
  222. if (!bridge)
  223. return NULL;
  224. atomic_set(&bridge->agp_in_use, 0);
  225. atomic_set(&bridge->current_memory_agp, 0);
  226. if (list_empty(&agp_bridges))
  227. agp_bridge = bridge;
  228. return bridge;
  229. }
  230. EXPORT_SYMBOL(agp_alloc_bridge);
  231. void agp_put_bridge(struct agp_bridge_data *bridge)
  232. {
  233. kfree(bridge);
  234. if (list_empty(&agp_bridges))
  235. agp_bridge = NULL;
  236. }
  237. EXPORT_SYMBOL(agp_put_bridge);
  238. int agp_add_bridge(struct agp_bridge_data *bridge)
  239. {
  240. int error;
  241. if (agp_off) {
  242. error = -ENODEV;
  243. goto err_put_bridge;
  244. }
  245. if (!bridge->dev) {
  246. printk (KERN_DEBUG PFX "Erk, registering with no pci_dev!\n");
  247. error = -EINVAL;
  248. goto err_put_bridge;
  249. }
  250. /* Grab reference on the chipset driver. */
  251. if (!try_module_get(bridge->driver->owner)) {
  252. dev_info(&bridge->dev->dev, "can't lock chipset driver\n");
  253. error = -EINVAL;
  254. goto err_put_bridge;
  255. }
  256. error = agp_backend_initialize(bridge);
  257. if (error) {
  258. dev_info(&bridge->dev->dev,
  259. "agp_backend_initialize() failed\n");
  260. goto err_out;
  261. }
  262. if (list_empty(&agp_bridges)) {
  263. error = agp_frontend_initialize();
  264. if (error) {
  265. dev_info(&bridge->dev->dev,
  266. "agp_frontend_initialize() failed\n");
  267. goto frontend_err;
  268. }
  269. dev_info(&bridge->dev->dev, "AGP aperture is %dM @ 0x%lx\n",
  270. bridge->driver->fetch_size(), bridge->gart_bus_addr);
  271. }
  272. list_add(&bridge->list, &agp_bridges);
  273. return 0;
  274. frontend_err:
  275. agp_backend_cleanup(bridge);
  276. err_out:
  277. module_put(bridge->driver->owner);
  278. err_put_bridge:
  279. agp_put_bridge(bridge);
  280. return error;
  281. }
  282. EXPORT_SYMBOL_GPL(agp_add_bridge);
  283. void agp_remove_bridge(struct agp_bridge_data *bridge)
  284. {
  285. agp_backend_cleanup(bridge);
  286. list_del(&bridge->list);
  287. if (list_empty(&agp_bridges))
  288. agp_frontend_cleanup();
  289. module_put(bridge->driver->owner);
  290. }
  291. EXPORT_SYMBOL_GPL(agp_remove_bridge);
  292. int agp_off;
  293. int agp_try_unsupported_boot;
  294. EXPORT_SYMBOL(agp_off);
  295. EXPORT_SYMBOL(agp_try_unsupported_boot);
  296. static int __init agp_init(void)
  297. {
  298. if (!agp_off)
  299. printk(KERN_INFO "Linux agpgart interface v%d.%d\n",
  300. AGPGART_VERSION_MAJOR, AGPGART_VERSION_MINOR);
  301. return 0;
  302. }
  303. static void __exit agp_exit(void)
  304. {
  305. }
  306. #ifndef MODULE
  307. static __init int agp_setup(char *s)
  308. {
  309. if (!strcmp(s,"off"))
  310. agp_off = 1;
  311. if (!strcmp(s,"try_unsupported"))
  312. agp_try_unsupported_boot = 1;
  313. return 1;
  314. }
  315. __setup("agp=", agp_setup);
  316. #endif
  317. MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
  318. MODULE_DESCRIPTION("AGP GART driver");
  319. MODULE_LICENSE("GPL and additional rights");
  320. MODULE_ALIAS_MISCDEV(AGPGART_MINOR);
  321. module_init(agp_init);
  322. module_exit(agp_exit);