drm_agpsupport.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. * \file drm_agpsupport.c
  3. * DRM support for AGP/GART backend
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  10. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  11. * All Rights Reserved.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice (including the next
  21. * paragraph) shall be included in all copies or substantial portions of the
  22. * Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. * OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. #include "drmP.h"
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #if __OS_HAS_AGP
  36. #include <asm/agp.h>
  37. /**
  38. * Get AGP information.
  39. *
  40. * \param inode device inode.
  41. * \param file_priv DRM file private.
  42. * \param cmd command.
  43. * \param arg pointer to a (output) drm_agp_info structure.
  44. * \return zero on success or a negative number on failure.
  45. *
  46. * Verifies the AGP device has been initialized and acquired and fills in the
  47. * drm_agp_info structure with the information in drm_agp_head::agp_info.
  48. */
  49. int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
  50. {
  51. DRM_AGP_KERN *kern;
  52. if (!dev->agp || !dev->agp->acquired)
  53. return -EINVAL;
  54. kern = &dev->agp->agp_info;
  55. info->agp_version_major = kern->version.major;
  56. info->agp_version_minor = kern->version.minor;
  57. info->mode = kern->mode;
  58. info->aperture_base = kern->aper_base;
  59. info->aperture_size = kern->aper_size * 1024 * 1024;
  60. info->memory_allowed = kern->max_memory << PAGE_SHIFT;
  61. info->memory_used = kern->current_memory << PAGE_SHIFT;
  62. info->id_vendor = kern->device->vendor;
  63. info->id_device = kern->device->device;
  64. return 0;
  65. }
  66. EXPORT_SYMBOL(drm_agp_info);
  67. int drm_agp_info_ioctl(struct drm_device *dev, void *data,
  68. struct drm_file *file_priv)
  69. {
  70. struct drm_agp_info *info = data;
  71. int err;
  72. err = drm_agp_info(dev, info);
  73. if (err)
  74. return err;
  75. return 0;
  76. }
  77. /**
  78. * Acquire the AGP device.
  79. *
  80. * \param dev DRM device that is to acquire AGP.
  81. * \return zero on success or a negative number on failure.
  82. *
  83. * Verifies the AGP device hasn't been acquired before and calls
  84. * \c agp_backend_acquire.
  85. */
  86. int drm_agp_acquire(struct drm_device * dev)
  87. {
  88. if (!dev->agp)
  89. return -ENODEV;
  90. if (dev->agp->acquired)
  91. return -EBUSY;
  92. if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
  93. return -ENODEV;
  94. dev->agp->acquired = 1;
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(drm_agp_acquire);
  98. /**
  99. * Acquire the AGP device (ioctl).
  100. *
  101. * \param inode device inode.
  102. * \param file_priv DRM file private.
  103. * \param cmd command.
  104. * \param arg user argument.
  105. * \return zero on success or a negative number on failure.
  106. *
  107. * Verifies the AGP device hasn't been acquired before and calls
  108. * \c agp_backend_acquire.
  109. */
  110. int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
  111. struct drm_file *file_priv)
  112. {
  113. return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
  114. }
  115. /**
  116. * Release the AGP device.
  117. *
  118. * \param dev DRM device that is to release AGP.
  119. * \return zero on success or a negative number on failure.
  120. *
  121. * Verifies the AGP device has been acquired and calls \c agp_backend_release.
  122. */
  123. int drm_agp_release(struct drm_device * dev)
  124. {
  125. if (!dev->agp || !dev->agp->acquired)
  126. return -EINVAL;
  127. agp_backend_release(dev->agp->bridge);
  128. dev->agp->acquired = 0;
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(drm_agp_release);
  132. int drm_agp_release_ioctl(struct drm_device *dev, void *data,
  133. struct drm_file *file_priv)
  134. {
  135. return drm_agp_release(dev);
  136. }
  137. /**
  138. * Enable the AGP bus.
  139. *
  140. * \param dev DRM device that has previously acquired AGP.
  141. * \param mode Requested AGP mode.
  142. * \return zero on success or a negative number on failure.
  143. *
  144. * Verifies the AGP device has been acquired but not enabled, and calls
  145. * \c agp_enable.
  146. */
  147. int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
  148. {
  149. if (!dev->agp || !dev->agp->acquired)
  150. return -EINVAL;
  151. dev->agp->mode = mode.mode;
  152. agp_enable(dev->agp->bridge, mode.mode);
  153. dev->agp->enabled = 1;
  154. return 0;
  155. }
  156. EXPORT_SYMBOL(drm_agp_enable);
  157. int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
  158. struct drm_file *file_priv)
  159. {
  160. struct drm_agp_mode *mode = data;
  161. return drm_agp_enable(dev, *mode);
  162. }
  163. /**
  164. * Allocate AGP memory.
  165. *
  166. * \param inode device inode.
  167. * \param file_priv file private pointer.
  168. * \param cmd command.
  169. * \param arg pointer to a drm_agp_buffer structure.
  170. * \return zero on success or a negative number on failure.
  171. *
  172. * Verifies the AGP device is present and has been acquired, allocates the
  173. * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
  174. */
  175. int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
  176. {
  177. struct drm_agp_mem *entry;
  178. DRM_AGP_MEM *memory;
  179. unsigned long pages;
  180. u32 type;
  181. if (!dev->agp || !dev->agp->acquired)
  182. return -EINVAL;
  183. if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
  184. return -ENOMEM;
  185. memset(entry, 0, sizeof(*entry));
  186. pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
  187. type = (u32) request->type;
  188. if (!(memory = agp_allocate_memory(dev->agp->bridge, pages, type))) {
  189. kfree(entry);
  190. return -ENOMEM;
  191. }
  192. entry->handle = (unsigned long)memory->key + 1;
  193. entry->memory = memory;
  194. entry->bound = 0;
  195. entry->pages = pages;
  196. list_add(&entry->head, &dev->agp->memory);
  197. request->handle = entry->handle;
  198. request->physical = memory->physical;
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(drm_agp_alloc);
  202. int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
  203. struct drm_file *file_priv)
  204. {
  205. struct drm_agp_buffer *request = data;
  206. return drm_agp_alloc(dev, request);
  207. }
  208. /**
  209. * Search for the AGP memory entry associated with a handle.
  210. *
  211. * \param dev DRM device structure.
  212. * \param handle AGP memory handle.
  213. * \return pointer to the drm_agp_mem structure associated with \p handle.
  214. *
  215. * Walks through drm_agp_head::memory until finding a matching handle.
  216. */
  217. static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
  218. unsigned long handle)
  219. {
  220. struct drm_agp_mem *entry;
  221. list_for_each_entry(entry, &dev->agp->memory, head) {
  222. if (entry->handle == handle)
  223. return entry;
  224. }
  225. return NULL;
  226. }
  227. /**
  228. * Unbind AGP memory from the GATT (ioctl).
  229. *
  230. * \param inode device inode.
  231. * \param file_priv DRM file private.
  232. * \param cmd command.
  233. * \param arg pointer to a drm_agp_binding structure.
  234. * \return zero on success or a negative number on failure.
  235. *
  236. * Verifies the AGP device is present and acquired, looks-up the AGP memory
  237. * entry and passes it to the unbind_agp() function.
  238. */
  239. int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
  240. {
  241. struct drm_agp_mem *entry;
  242. int ret;
  243. if (!dev->agp || !dev->agp->acquired)
  244. return -EINVAL;
  245. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  246. return -EINVAL;
  247. if (!entry->bound)
  248. return -EINVAL;
  249. ret = drm_unbind_agp(entry->memory);
  250. if (ret == 0)
  251. entry->bound = 0;
  252. return ret;
  253. }
  254. EXPORT_SYMBOL(drm_agp_unbind);
  255. int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
  256. struct drm_file *file_priv)
  257. {
  258. struct drm_agp_binding *request = data;
  259. return drm_agp_unbind(dev, request);
  260. }
  261. /**
  262. * Bind AGP memory into the GATT (ioctl)
  263. *
  264. * \param inode device inode.
  265. * \param file_priv DRM file private.
  266. * \param cmd command.
  267. * \param arg pointer to a drm_agp_binding structure.
  268. * \return zero on success or a negative number on failure.
  269. *
  270. * Verifies the AGP device is present and has been acquired and that no memory
  271. * is currently bound into the GATT. Looks-up the AGP memory entry and passes
  272. * it to bind_agp() function.
  273. */
  274. int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
  275. {
  276. struct drm_agp_mem *entry;
  277. int retcode;
  278. int page;
  279. if (!dev->agp || !dev->agp->acquired)
  280. return -EINVAL;
  281. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  282. return -EINVAL;
  283. if (entry->bound)
  284. return -EINVAL;
  285. page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
  286. if ((retcode = drm_bind_agp(entry->memory, page)))
  287. return retcode;
  288. entry->bound = dev->agp->base + (page << PAGE_SHIFT);
  289. DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
  290. dev->agp->base, entry->bound);
  291. return 0;
  292. }
  293. EXPORT_SYMBOL(drm_agp_bind);
  294. int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
  295. struct drm_file *file_priv)
  296. {
  297. struct drm_agp_binding *request = data;
  298. return drm_agp_bind(dev, request);
  299. }
  300. /**
  301. * Free AGP memory (ioctl).
  302. *
  303. * \param inode device inode.
  304. * \param file_priv DRM file private.
  305. * \param cmd command.
  306. * \param arg pointer to a drm_agp_buffer structure.
  307. * \return zero on success or a negative number on failure.
  308. *
  309. * Verifies the AGP device is present and has been acquired and looks up the
  310. * AGP memory entry. If the memory it's currently bound, unbind it via
  311. * unbind_agp(). Frees it via free_agp() as well as the entry itself
  312. * and unlinks from the doubly linked list it's inserted in.
  313. */
  314. int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
  315. {
  316. struct drm_agp_mem *entry;
  317. if (!dev->agp || !dev->agp->acquired)
  318. return -EINVAL;
  319. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  320. return -EINVAL;
  321. if (entry->bound)
  322. drm_unbind_agp(entry->memory);
  323. list_del(&entry->head);
  324. drm_free_agp(entry->memory, entry->pages);
  325. kfree(entry);
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(drm_agp_free);
  329. int drm_agp_free_ioctl(struct drm_device *dev, void *data,
  330. struct drm_file *file_priv)
  331. {
  332. struct drm_agp_buffer *request = data;
  333. return drm_agp_free(dev, request);
  334. }
  335. /**
  336. * Initialize the AGP resources.
  337. *
  338. * \return pointer to a drm_agp_head structure.
  339. *
  340. * Gets the drm_agp_t structure which is made available by the agpgart module
  341. * via the inter_module_* functions. Creates and initializes a drm_agp_head
  342. * structure.
  343. */
  344. struct drm_agp_head *drm_agp_init(struct drm_device *dev)
  345. {
  346. struct drm_agp_head *head = NULL;
  347. if (!(head = kmalloc(sizeof(*head), GFP_KERNEL)))
  348. return NULL;
  349. memset((void *)head, 0, sizeof(*head));
  350. head->bridge = agp_find_bridge(dev->pdev);
  351. if (!head->bridge) {
  352. if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
  353. kfree(head);
  354. return NULL;
  355. }
  356. agp_copy_info(head->bridge, &head->agp_info);
  357. agp_backend_release(head->bridge);
  358. } else {
  359. agp_copy_info(head->bridge, &head->agp_info);
  360. }
  361. if (head->agp_info.chipset == NOT_SUPPORTED) {
  362. kfree(head);
  363. return NULL;
  364. }
  365. INIT_LIST_HEAD(&head->memory);
  366. head->cant_use_aperture = head->agp_info.cant_use_aperture;
  367. head->page_mask = head->agp_info.page_mask;
  368. head->base = head->agp_info.aper_base;
  369. return head;
  370. }
  371. /**
  372. * Binds a collection of pages into AGP memory at the given offset, returning
  373. * the AGP memory structure containing them.
  374. *
  375. * No reference is held on the pages during this time -- it is up to the
  376. * caller to handle that.
  377. */
  378. DRM_AGP_MEM *
  379. drm_agp_bind_pages(struct drm_device *dev,
  380. struct page **pages,
  381. unsigned long num_pages,
  382. uint32_t gtt_offset,
  383. u32 type)
  384. {
  385. DRM_AGP_MEM *mem;
  386. int ret, i;
  387. DRM_DEBUG("\n");
  388. mem = agp_allocate_memory(dev->agp->bridge, num_pages,
  389. type);
  390. if (mem == NULL) {
  391. DRM_ERROR("Failed to allocate memory for %ld pages\n",
  392. num_pages);
  393. return NULL;
  394. }
  395. for (i = 0; i < num_pages; i++)
  396. mem->pages[i] = pages[i];
  397. mem->page_count = num_pages;
  398. mem->is_flushed = true;
  399. ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
  400. if (ret != 0) {
  401. DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
  402. agp_free_memory(mem);
  403. return NULL;
  404. }
  405. return mem;
  406. }
  407. EXPORT_SYMBOL(drm_agp_bind_pages);
  408. #endif /* __OS_HAS_AGP */