drm_agpsupport.c 13 KB

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