drm_agpsupport.c 12 KB

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