drm_agpsupport.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 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 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 inode *inode, struct file *filp,
  66. unsigned int cmd, unsigned long arg)
  67. {
  68. struct drm_file *priv = filp->private_data;
  69. struct drm_device *dev = priv->head->dev;
  70. struct drm_agp_info info;
  71. int err;
  72. err = drm_agp_info(dev, &info);
  73. if (err)
  74. return err;
  75. if (copy_to_user((struct drm_agp_info __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(struct drm_device * 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. struct drm_file *priv = filp->private_data;
  116. return drm_agp_acquire((struct drm_device *) 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(struct drm_device * 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. struct drm_file *priv = filp->private_data;
  139. struct drm_device *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(struct drm_device * dev, struct drm_agp_mode 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. struct drm_file *priv = filp->private_data;
  167. struct drm_device *dev = priv->head->dev;
  168. struct drm_agp_mode mode;
  169. if (copy_from_user(&mode, (struct drm_agp_mode __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 drm_device *dev, struct drm_agp_buffer *request)
  186. {
  187. struct drm_agp_mem *entry;
  188. DRM_AGP_MEM *memory;
  189. unsigned long pages;
  190. u32 type;
  191. if (!dev->agp || !dev->agp->acquired)
  192. return -EINVAL;
  193. if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS)))
  194. return -ENOMEM;
  195. memset(entry, 0, sizeof(*entry));
  196. pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
  197. type = (u32) request->type;
  198. if (!(memory = drm_alloc_agp(dev, pages, type))) {
  199. drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
  200. return -ENOMEM;
  201. }
  202. entry->handle = (unsigned long)memory->key + 1;
  203. entry->memory = memory;
  204. entry->bound = 0;
  205. entry->pages = pages;
  206. list_add(&entry->head, &dev->agp->memory);
  207. request->handle = entry->handle;
  208. request->physical = memory->physical;
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(drm_agp_alloc);
  212. int drm_agp_alloc_ioctl(struct inode *inode, struct file *filp,
  213. unsigned int cmd, unsigned long arg)
  214. {
  215. struct drm_file *priv = filp->private_data;
  216. struct drm_device *dev = priv->head->dev;
  217. struct drm_agp_buffer request;
  218. struct drm_agp_buffer __user *argp = (void __user *)arg;
  219. int err;
  220. if (copy_from_user(&request, argp, sizeof(request)))
  221. return -EFAULT;
  222. err = drm_agp_alloc(dev, &request);
  223. if (err)
  224. return err;
  225. if (copy_to_user(argp, &request, sizeof(request))) {
  226. struct drm_agp_mem *entry;
  227. list_for_each_entry(entry, &dev->agp->memory, head) {
  228. if (entry->handle == request.handle)
  229. break;
  230. }
  231. list_del(&entry->head);
  232. drm_free_agp(entry->memory, entry->pages);
  233. drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
  234. return -EFAULT;
  235. }
  236. return 0;
  237. }
  238. /**
  239. * Search for the AGP memory entry associated with a handle.
  240. *
  241. * \param dev DRM device structure.
  242. * \param handle AGP memory handle.
  243. * \return pointer to the drm_agp_mem structure associated with \p handle.
  244. *
  245. * Walks through drm_agp_head::memory until finding a matching handle.
  246. */
  247. static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
  248. unsigned long handle)
  249. {
  250. struct drm_agp_mem *entry;
  251. list_for_each_entry(entry, &dev->agp->memory, head) {
  252. if (entry->handle == handle)
  253. return entry;
  254. }
  255. return NULL;
  256. }
  257. /**
  258. * Unbind AGP memory from the GATT (ioctl).
  259. *
  260. * \param inode device inode.
  261. * \param filp file pointer.
  262. * \param cmd command.
  263. * \param arg pointer to a drm_agp_binding structure.
  264. * \return zero on success or a negative number on failure.
  265. *
  266. * Verifies the AGP device is present and acquired, looks-up the AGP memory
  267. * entry and passes it to the unbind_agp() function.
  268. */
  269. int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
  270. {
  271. struct drm_agp_mem *entry;
  272. int ret;
  273. if (!dev->agp || !dev->agp->acquired)
  274. return -EINVAL;
  275. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  276. return -EINVAL;
  277. if (!entry->bound)
  278. return -EINVAL;
  279. ret = drm_unbind_agp(entry->memory);
  280. if (ret == 0)
  281. entry->bound = 0;
  282. return ret;
  283. }
  284. EXPORT_SYMBOL(drm_agp_unbind);
  285. int drm_agp_unbind_ioctl(struct inode *inode, struct file *filp,
  286. unsigned int cmd, unsigned long arg)
  287. {
  288. struct drm_file *priv = filp->private_data;
  289. struct drm_device *dev = priv->head->dev;
  290. struct drm_agp_binding request;
  291. if (copy_from_user
  292. (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
  293. return -EFAULT;
  294. return drm_agp_unbind(dev, &request);
  295. }
  296. /**
  297. * Bind AGP memory into the GATT (ioctl)
  298. *
  299. * \param inode device inode.
  300. * \param filp file pointer.
  301. * \param cmd command.
  302. * \param arg pointer to a drm_agp_binding structure.
  303. * \return zero on success or a negative number on failure.
  304. *
  305. * Verifies the AGP device is present and has been acquired and that no memory
  306. * is currently bound into the GATT. Looks-up the AGP memory entry and passes
  307. * it to bind_agp() function.
  308. */
  309. int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
  310. {
  311. struct drm_agp_mem *entry;
  312. int retcode;
  313. int page;
  314. if (!dev->agp || !dev->agp->acquired)
  315. return -EINVAL;
  316. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  317. return -EINVAL;
  318. if (entry->bound)
  319. return -EINVAL;
  320. page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
  321. if ((retcode = drm_bind_agp(entry->memory, page)))
  322. return retcode;
  323. entry->bound = dev->agp->base + (page << PAGE_SHIFT);
  324. DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
  325. dev->agp->base, entry->bound);
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(drm_agp_bind);
  329. int drm_agp_bind_ioctl(struct inode *inode, struct file *filp,
  330. unsigned int cmd, unsigned long arg)
  331. {
  332. struct drm_file *priv = filp->private_data;
  333. struct drm_device *dev = priv->head->dev;
  334. struct drm_agp_binding request;
  335. if (copy_from_user
  336. (&request, (struct drm_agp_binding __user *) arg, sizeof(request)))
  337. return -EFAULT;
  338. return drm_agp_bind(dev, &request);
  339. }
  340. /**
  341. * Free AGP memory (ioctl).
  342. *
  343. * \param inode device inode.
  344. * \param filp file pointer.
  345. * \param cmd command.
  346. * \param arg pointer to a drm_agp_buffer structure.
  347. * \return zero on success or a negative number on failure.
  348. *
  349. * Verifies the AGP device is present and has been acquired and looks up the
  350. * AGP memory entry. If the memory it's currently bound, unbind it via
  351. * unbind_agp(). Frees it via free_agp() as well as the entry itself
  352. * and unlinks from the doubly linked list it's inserted in.
  353. */
  354. int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
  355. {
  356. struct drm_agp_mem *entry;
  357. if (!dev->agp || !dev->agp->acquired)
  358. return -EINVAL;
  359. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  360. return -EINVAL;
  361. if (entry->bound)
  362. drm_unbind_agp(entry->memory);
  363. list_del(&entry->head);
  364. drm_free_agp(entry->memory, entry->pages);
  365. drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
  366. return 0;
  367. }
  368. EXPORT_SYMBOL(drm_agp_free);
  369. int drm_agp_free_ioctl(struct inode *inode, struct file *filp,
  370. unsigned int cmd, unsigned long arg)
  371. {
  372. struct drm_file *priv = filp->private_data;
  373. struct drm_device *dev = priv->head->dev;
  374. struct drm_agp_buffer request;
  375. if (copy_from_user
  376. (&request, (struct drm_agp_buffer __user *) arg, sizeof(request)))
  377. return -EFAULT;
  378. return drm_agp_free(dev, &request);
  379. }
  380. /**
  381. * Initialize the AGP resources.
  382. *
  383. * \return pointer to a drm_agp_head structure.
  384. *
  385. * Gets the drm_agp_t structure which is made available by the agpgart module
  386. * via the inter_module_* functions. Creates and initializes a drm_agp_head
  387. * structure.
  388. */
  389. struct drm_agp_head *drm_agp_init(struct drm_device *dev)
  390. {
  391. struct drm_agp_head *head = NULL;
  392. if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS)))
  393. return NULL;
  394. memset((void *)head, 0, sizeof(*head));
  395. head->bridge = agp_find_bridge(dev->pdev);
  396. if (!head->bridge) {
  397. if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
  398. drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
  399. return NULL;
  400. }
  401. agp_copy_info(head->bridge, &head->agp_info);
  402. agp_backend_release(head->bridge);
  403. } else {
  404. agp_copy_info(head->bridge, &head->agp_info);
  405. }
  406. if (head->agp_info.chipset == NOT_SUPPORTED) {
  407. drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS);
  408. return NULL;
  409. }
  410. INIT_LIST_HEAD(&head->memory);
  411. head->cant_use_aperture = head->agp_info.cant_use_aperture;
  412. head->page_mask = head->agp_info.page_mask;
  413. return head;
  414. }
  415. /** Calls agp_allocate_memory() */
  416. DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data * bridge,
  417. size_t pages, u32 type)
  418. {
  419. return agp_allocate_memory(bridge, pages, type);
  420. }
  421. /** Calls agp_free_memory() */
  422. int drm_agp_free_memory(DRM_AGP_MEM * handle)
  423. {
  424. if (!handle)
  425. return 0;
  426. agp_free_memory(handle);
  427. return 1;
  428. }
  429. /** Calls agp_bind_memory() */
  430. int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start)
  431. {
  432. if (!handle)
  433. return -EINVAL;
  434. return agp_bind_memory(handle, start);
  435. }
  436. /** Calls agp_unbind_memory() */
  437. int drm_agp_unbind_memory(DRM_AGP_MEM * handle)
  438. {
  439. if (!handle)
  440. return -EINVAL;
  441. return agp_unbind_memory(handle);
  442. }
  443. #endif /* __OS_HAS_AGP */