drm_agpsupport.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #if __OS_HAS_AGP
  35. /**
  36. * Get AGP information.
  37. *
  38. * \param inode device inode.
  39. * \param file_priv DRM file private.
  40. * \param cmd command.
  41. * \param arg pointer to a (output) drm_agp_info structure.
  42. * \return zero on success or a negative number on failure.
  43. *
  44. * Verifies the AGP device has been initialized and acquired and fills in the
  45. * drm_agp_info structure with the information in drm_agp_head::agp_info.
  46. */
  47. int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
  48. {
  49. DRM_AGP_KERN *kern;
  50. if (!dev->agp || !dev->agp->acquired)
  51. return -EINVAL;
  52. kern = &dev->agp->agp_info;
  53. info->agp_version_major = kern->version.major;
  54. info->agp_version_minor = kern->version.minor;
  55. info->mode = kern->mode;
  56. info->aperture_base = kern->aper_base;
  57. info->aperture_size = kern->aper_size * 1024 * 1024;
  58. info->memory_allowed = kern->max_memory << PAGE_SHIFT;
  59. info->memory_used = kern->current_memory << PAGE_SHIFT;
  60. info->id_vendor = kern->device->vendor;
  61. info->id_device = kern->device->device;
  62. return 0;
  63. }
  64. EXPORT_SYMBOL(drm_agp_info);
  65. int drm_agp_info_ioctl(struct drm_device *dev, void *data,
  66. struct drm_file *file_priv)
  67. {
  68. struct drm_agp_info *info = data;
  69. int err;
  70. err = drm_agp_info(dev, info);
  71. if (err)
  72. return err;
  73. return 0;
  74. }
  75. /**
  76. * Acquire the AGP device.
  77. *
  78. * \param dev DRM device that is to acquire AGP.
  79. * \return zero on success or a negative number on failure.
  80. *
  81. * Verifies the AGP device hasn't been acquired before and calls
  82. * \c agp_backend_acquire.
  83. */
  84. int drm_agp_acquire(struct drm_device * dev)
  85. {
  86. if (!dev->agp)
  87. return -ENODEV;
  88. if (dev->agp->acquired)
  89. return -EBUSY;
  90. if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
  91. return -ENODEV;
  92. dev->agp->acquired = 1;
  93. return 0;
  94. }
  95. EXPORT_SYMBOL(drm_agp_acquire);
  96. /**
  97. * Acquire the AGP device (ioctl).
  98. *
  99. * \param inode device inode.
  100. * \param file_priv DRM file private.
  101. * \param cmd command.
  102. * \param arg user argument.
  103. * \return zero on success or a negative number on failure.
  104. *
  105. * Verifies the AGP device hasn't been acquired before and calls
  106. * \c agp_backend_acquire.
  107. */
  108. int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
  109. struct drm_file *file_priv)
  110. {
  111. return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
  112. }
  113. /**
  114. * Release the AGP device.
  115. *
  116. * \param dev DRM device that is to release AGP.
  117. * \return zero on success or a negative number on failure.
  118. *
  119. * Verifies the AGP device has been acquired and calls \c agp_backend_release.
  120. */
  121. int drm_agp_release(struct drm_device * dev)
  122. {
  123. if (!dev->agp || !dev->agp->acquired)
  124. return -EINVAL;
  125. agp_backend_release(dev->agp->bridge);
  126. dev->agp->acquired = 0;
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(drm_agp_release);
  130. int drm_agp_release_ioctl(struct drm_device *dev, void *data,
  131. struct drm_file *file_priv)
  132. {
  133. return drm_agp_release(dev);
  134. }
  135. /**
  136. * Enable the AGP bus.
  137. *
  138. * \param dev DRM device that has previously acquired AGP.
  139. * \param mode Requested AGP mode.
  140. * \return zero on success or a negative number on failure.
  141. *
  142. * Verifies the AGP device has been acquired but not enabled, and calls
  143. * \c agp_enable.
  144. */
  145. int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode)
  146. {
  147. if (!dev->agp || !dev->agp->acquired)
  148. return -EINVAL;
  149. dev->agp->mode = mode.mode;
  150. agp_enable(dev->agp->bridge, mode.mode);
  151. dev->agp->enabled = 1;
  152. return 0;
  153. }
  154. EXPORT_SYMBOL(drm_agp_enable);
  155. int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
  156. struct drm_file *file_priv)
  157. {
  158. struct drm_agp_mode *mode = data;
  159. return drm_agp_enable(dev, *mode);
  160. }
  161. /**
  162. * Allocate AGP memory.
  163. *
  164. * \param inode device inode.
  165. * \param file_priv file private pointer.
  166. * \param cmd command.
  167. * \param arg pointer to a drm_agp_buffer structure.
  168. * \return zero on success or a negative number on failure.
  169. *
  170. * Verifies the AGP device is present and has been acquired, allocates the
  171. * memory via alloc_agp() and creates a drm_agp_mem entry for it.
  172. */
  173. int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
  174. {
  175. struct drm_agp_mem *entry;
  176. DRM_AGP_MEM *memory;
  177. unsigned long pages;
  178. u32 type;
  179. if (!dev->agp || !dev->agp->acquired)
  180. return -EINVAL;
  181. if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
  182. return -ENOMEM;
  183. memset(entry, 0, sizeof(*entry));
  184. pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
  185. type = (u32) request->type;
  186. if (!(memory = drm_alloc_agp(dev, pages, type))) {
  187. drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
  188. return -ENOMEM;
  189. }
  190. entry->handle = (unsigned long)memory->key + 1;
  191. entry->memory = memory;
  192. entry->bound = 0;
  193. entry->pages = pages;
  194. list_add(&entry->head, &dev->agp->memory);
  195. request->handle = entry->handle;
  196. request->physical = memory->physical;
  197. return 0;
  198. }
  199. EXPORT_SYMBOL(drm_agp_alloc);
  200. int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
  201. struct drm_file *file_priv)
  202. {
  203. struct drm_agp_buffer *request = data;
  204. return drm_agp_alloc(dev, request);
  205. }
  206. /**
  207. * Search for the AGP memory entry associated with a handle.
  208. *
  209. * \param dev DRM device structure.
  210. * \param handle AGP memory handle.
  211. * \return pointer to the drm_agp_mem structure associated with \p handle.
  212. *
  213. * Walks through drm_agp_head::memory until finding a matching handle.
  214. */
  215. static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
  216. unsigned long handle)
  217. {
  218. struct drm_agp_mem *entry;
  219. list_for_each_entry(entry, &dev->agp->memory, head) {
  220. if (entry->handle == handle)
  221. return entry;
  222. }
  223. return NULL;
  224. }
  225. /**
  226. * Unbind AGP memory from the GATT (ioctl).
  227. *
  228. * \param inode device inode.
  229. * \param file_priv DRM file private.
  230. * \param cmd command.
  231. * \param arg pointer to a drm_agp_binding structure.
  232. * \return zero on success or a negative number on failure.
  233. *
  234. * Verifies the AGP device is present and acquired, looks-up the AGP memory
  235. * entry and passes it to the unbind_agp() function.
  236. */
  237. int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
  238. {
  239. struct drm_agp_mem *entry;
  240. int ret;
  241. if (!dev->agp || !dev->agp->acquired)
  242. return -EINVAL;
  243. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  244. return -EINVAL;
  245. if (!entry->bound)
  246. return -EINVAL;
  247. ret = drm_unbind_agp(entry->memory);
  248. if (ret == 0)
  249. entry->bound = 0;
  250. return ret;
  251. }
  252. EXPORT_SYMBOL(drm_agp_unbind);
  253. int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
  254. struct drm_file *file_priv)
  255. {
  256. struct drm_agp_binding *request = data;
  257. return drm_agp_unbind(dev, request);
  258. }
  259. /**
  260. * Bind AGP memory into the GATT (ioctl)
  261. *
  262. * \param inode device inode.
  263. * \param file_priv DRM file private.
  264. * \param cmd command.
  265. * \param arg pointer to a drm_agp_binding structure.
  266. * \return zero on success or a negative number on failure.
  267. *
  268. * Verifies the AGP device is present and has been acquired and that no memory
  269. * is currently bound into the GATT. Looks-up the AGP memory entry and passes
  270. * it to bind_agp() function.
  271. */
  272. int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
  273. {
  274. struct drm_agp_mem *entry;
  275. int retcode;
  276. int page;
  277. if (!dev->agp || !dev->agp->acquired)
  278. return -EINVAL;
  279. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  280. return -EINVAL;
  281. if (entry->bound)
  282. return -EINVAL;
  283. page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
  284. if ((retcode = drm_bind_agp(entry->memory, page)))
  285. return retcode;
  286. entry->bound = dev->agp->base + (page << PAGE_SHIFT);
  287. DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
  288. dev->agp->base, entry->bound);
  289. return 0;
  290. }
  291. EXPORT_SYMBOL(drm_agp_bind);
  292. int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
  293. struct drm_file *file_priv)
  294. {
  295. struct drm_agp_binding *request = data;
  296. return drm_agp_bind(dev, request);
  297. }
  298. /**
  299. * Free AGP memory (ioctl).
  300. *
  301. * \param inode device inode.
  302. * \param file_priv DRM file private.
  303. * \param cmd command.
  304. * \param arg pointer to a drm_agp_buffer structure.
  305. * \return zero on success or a negative number on failure.
  306. *
  307. * Verifies the AGP device is present and has been acquired and looks up the
  308. * AGP memory entry. If the memory it's currently bound, unbind it via
  309. * unbind_agp(). Frees it via free_agp() as well as the entry itself
  310. * and unlinks from the doubly linked list it's inserted in.
  311. */
  312. int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
  313. {
  314. struct drm_agp_mem *entry;
  315. if (!dev->agp || !dev->agp->acquired)
  316. return -EINVAL;
  317. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  318. return -EINVAL;
  319. if (entry->bound)
  320. drm_unbind_agp(entry->memory);
  321. list_del(&entry->head);
  322. drm_free_agp(entry->memory, entry->pages);
  323. drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
  324. return 0;
  325. }
  326. EXPORT_SYMBOL(drm_agp_free);
  327. int drm_agp_free_ioctl(struct drm_device *dev, void *data,
  328. struct drm_file *file_priv)
  329. {
  330. struct drm_agp_buffer *request = data;
  331. return drm_agp_free(dev, request);
  332. }
  333. /**
  334. * Initialize the AGP resources.
  335. *
  336. * \return pointer to a drm_agp_head structure.
  337. *
  338. * Gets the drm_agp_t structure which is made available by the agpgart module
  339. * via the inter_module_* functions. Creates and initializes a drm_agp_head
  340. * structure.
  341. */
  342. struct drm_agp_head *drm_agp_init(struct drm_device *dev)
  343. {
  344. struct drm_agp_head *head = NULL;
  345. if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
  346. return NULL;
  347. memset((void *)head, 0, sizeof(*head));
  348. head->bridge = agp_find_bridge(dev->pdev);
  349. if (!head->bridge) {
  350. if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
  351. drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
  352. return NULL;
  353. }
  354. agp_copy_info(head->bridge, &head->agp_info);
  355. agp_backend_release(head->bridge);
  356. } else {
  357. agp_copy_info(head->bridge, &head->agp_info);
  358. }
  359. if (head->agp_info.chipset == NOT_SUPPORTED) {
  360. drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
  361. return NULL;
  362. }
  363. INIT_LIST_HEAD(&head->memory);
  364. head->cant_use_aperture = head->agp_info.cant_use_aperture;
  365. head->page_mask = head->agp_info.page_mask;
  366. head->base = head->agp_info.aper_base;
  367. return head;
  368. }
  369. /** Calls agp_allocate_memory() */
  370. DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data * bridge,
  371. size_t pages, u32 type)
  372. {
  373. return agp_allocate_memory(bridge, pages, type);
  374. }
  375. /** Calls agp_free_memory() */
  376. int drm_agp_free_memory(DRM_AGP_MEM * handle)
  377. {
  378. if (!handle)
  379. return 0;
  380. agp_free_memory(handle);
  381. return 1;
  382. }
  383. /** Calls agp_bind_memory() */
  384. int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
  385. {
  386. if (!handle)
  387. return -EINVAL;
  388. return agp_bind_memory(handle, start);
  389. }
  390. /** Calls agp_unbind_memory() */
  391. int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
  392. {
  393. if (!handle)
  394. return -EINVAL;
  395. return agp_unbind_memory(handle);
  396. }
  397. #endif /* __OS_HAS_AGP */