drm_agpsupport.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 <drm/drmP.h>
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include "drm_legacy.h"
  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. struct agp_kern_info *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. struct agp_memory *memory;
  179. unsigned long pages;
  180. u32 type;
  181. if (!dev->agp || !dev->agp->acquired)
  182. return -EINVAL;
  183. if (!(entry = kzalloc(sizeof(*entry), GFP_KERNEL)))
  184. return -ENOMEM;
  185. pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
  186. type = (u32) request->type;
  187. if (!(memory = agp_allocate_memory(dev->agp->bridge, pages, type))) {
  188. kfree(entry);
  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. list_add(&entry->head, &dev->agp->memory);
  196. request->handle = entry->handle;
  197. request->physical = memory->physical;
  198. return 0;
  199. }
  200. EXPORT_SYMBOL(drm_agp_alloc);
  201. int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
  202. struct drm_file *file_priv)
  203. {
  204. struct drm_agp_buffer *request = data;
  205. return drm_agp_alloc(dev, request);
  206. }
  207. /**
  208. * Search for the AGP memory entry associated with a handle.
  209. *
  210. * \param dev DRM device structure.
  211. * \param handle AGP memory handle.
  212. * \return pointer to the drm_agp_mem structure associated with \p handle.
  213. *
  214. * Walks through drm_agp_head::memory until finding a matching handle.
  215. */
  216. static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
  217. unsigned long handle)
  218. {
  219. struct drm_agp_mem *entry;
  220. list_for_each_entry(entry, &dev->agp->memory, head) {
  221. if (entry->handle == handle)
  222. return entry;
  223. }
  224. return NULL;
  225. }
  226. /**
  227. * Unbind AGP memory from the GATT (ioctl).
  228. *
  229. * \param inode device inode.
  230. * \param file_priv DRM file private.
  231. * \param cmd command.
  232. * \param arg pointer to a drm_agp_binding structure.
  233. * \return zero on success or a negative number on failure.
  234. *
  235. * Verifies the AGP device is present and acquired, looks-up the AGP memory
  236. * entry and passes it to the unbind_agp() function.
  237. */
  238. int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
  239. {
  240. struct drm_agp_mem *entry;
  241. int ret;
  242. if (!dev->agp || !dev->agp->acquired)
  243. return -EINVAL;
  244. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  245. return -EINVAL;
  246. if (!entry->bound)
  247. return -EINVAL;
  248. ret = drm_unbind_agp(entry->memory);
  249. if (ret == 0)
  250. entry->bound = 0;
  251. return ret;
  252. }
  253. EXPORT_SYMBOL(drm_agp_unbind);
  254. int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
  255. struct drm_file *file_priv)
  256. {
  257. struct drm_agp_binding *request = data;
  258. return drm_agp_unbind(dev, request);
  259. }
  260. /**
  261. * Bind AGP memory into the GATT (ioctl)
  262. *
  263. * \param inode device inode.
  264. * \param file_priv DRM file private.
  265. * \param cmd command.
  266. * \param arg pointer to a drm_agp_binding structure.
  267. * \return zero on success or a negative number on failure.
  268. *
  269. * Verifies the AGP device is present and has been acquired and that no memory
  270. * is currently bound into the GATT. Looks-up the AGP memory entry and passes
  271. * it to bind_agp() function.
  272. */
  273. int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
  274. {
  275. struct drm_agp_mem *entry;
  276. int retcode;
  277. int page;
  278. if (!dev->agp || !dev->agp->acquired)
  279. return -EINVAL;
  280. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  281. return -EINVAL;
  282. if (entry->bound)
  283. return -EINVAL;
  284. page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
  285. if ((retcode = drm_bind_agp(entry->memory, page)))
  286. return retcode;
  287. entry->bound = dev->agp->base + (page << PAGE_SHIFT);
  288. DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
  289. dev->agp->base, entry->bound);
  290. return 0;
  291. }
  292. EXPORT_SYMBOL(drm_agp_bind);
  293. int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
  294. struct drm_file *file_priv)
  295. {
  296. struct drm_agp_binding *request = data;
  297. return drm_agp_bind(dev, request);
  298. }
  299. /**
  300. * Free AGP memory (ioctl).
  301. *
  302. * \param inode device inode.
  303. * \param file_priv DRM file private.
  304. * \param cmd command.
  305. * \param arg pointer to a drm_agp_buffer structure.
  306. * \return zero on success or a negative number on failure.
  307. *
  308. * Verifies the AGP device is present and has been acquired and looks up the
  309. * AGP memory entry. If the memory it's currently bound, unbind it via
  310. * unbind_agp(). Frees it via free_agp() as well as the entry itself
  311. * and unlinks from the doubly linked list it's inserted in.
  312. */
  313. int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
  314. {
  315. struct drm_agp_mem *entry;
  316. if (!dev->agp || !dev->agp->acquired)
  317. return -EINVAL;
  318. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  319. return -EINVAL;
  320. if (entry->bound)
  321. drm_unbind_agp(entry->memory);
  322. list_del(&entry->head);
  323. drm_free_agp(entry->memory, entry->pages);
  324. kfree(entry);
  325. return 0;
  326. }
  327. EXPORT_SYMBOL(drm_agp_free);
  328. int drm_agp_free_ioctl(struct drm_device *dev, void *data,
  329. struct drm_file *file_priv)
  330. {
  331. struct drm_agp_buffer *request = data;
  332. return drm_agp_free(dev, request);
  333. }
  334. /**
  335. * Initialize the AGP resources.
  336. *
  337. * \return pointer to a drm_agp_head structure.
  338. *
  339. * Gets the drm_agp_t structure which is made available by the agpgart module
  340. * via the inter_module_* functions. Creates and initializes a drm_agp_head
  341. * structure.
  342. *
  343. * Note that final cleanup of the kmalloced structure is directly done in
  344. * drm_pci_agp_destroy.
  345. */
  346. struct drm_agp_head *drm_agp_init(struct drm_device *dev)
  347. {
  348. struct drm_agp_head *head = NULL;
  349. if (!(head = kzalloc(sizeof(*head), GFP_KERNEL)))
  350. return NULL;
  351. head->bridge = agp_find_bridge(dev->pdev);
  352. if (!head->bridge) {
  353. if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
  354. kfree(head);
  355. return NULL;
  356. }
  357. agp_copy_info(head->bridge, &head->agp_info);
  358. agp_backend_release(head->bridge);
  359. } else {
  360. agp_copy_info(head->bridge, &head->agp_info);
  361. }
  362. if (head->agp_info.chipset == NOT_SUPPORTED) {
  363. kfree(head);
  364. return NULL;
  365. }
  366. INIT_LIST_HEAD(&head->memory);
  367. head->cant_use_aperture = head->agp_info.cant_use_aperture;
  368. head->page_mask = head->agp_info.page_mask;
  369. head->base = head->agp_info.aper_base;
  370. return head;
  371. }
  372. /**
  373. * drm_agp_clear - Clear AGP resource list
  374. * @dev: DRM device
  375. *
  376. * Iterate over all AGP resources and remove them. But keep the AGP head
  377. * intact so it can still be used. It is safe to call this if AGP is disabled or
  378. * was already removed.
  379. *
  380. * If DRIVER_MODESET is active, nothing is done to protect the modesetting
  381. * resources from getting destroyed. Drivers are responsible of cleaning them up
  382. * during device shutdown.
  383. */
  384. void drm_agp_clear(struct drm_device *dev)
  385. {
  386. struct drm_agp_mem *entry, *tempe;
  387. if (!dev->agp)
  388. return;
  389. if (drm_core_check_feature(dev, DRIVER_MODESET))
  390. return;
  391. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  392. if (entry->bound)
  393. drm_unbind_agp(entry->memory);
  394. drm_free_agp(entry->memory, entry->pages);
  395. kfree(entry);
  396. }
  397. INIT_LIST_HEAD(&dev->agp->memory);
  398. if (dev->agp->acquired)
  399. drm_agp_release(dev);
  400. dev->agp->acquired = 0;
  401. dev->agp->enabled = 0;
  402. }
  403. /**
  404. * Binds a collection of pages into AGP memory at the given offset, returning
  405. * the AGP memory structure containing them.
  406. *
  407. * No reference is held on the pages during this time -- it is up to the
  408. * caller to handle that.
  409. */
  410. struct agp_memory *
  411. drm_agp_bind_pages(struct drm_device *dev,
  412. struct page **pages,
  413. unsigned long num_pages,
  414. uint32_t gtt_offset,
  415. u32 type)
  416. {
  417. struct agp_memory *mem;
  418. int ret, i;
  419. DRM_DEBUG("\n");
  420. mem = agp_allocate_memory(dev->agp->bridge, num_pages,
  421. type);
  422. if (mem == NULL) {
  423. DRM_ERROR("Failed to allocate memory for %ld pages\n",
  424. num_pages);
  425. return NULL;
  426. }
  427. for (i = 0; i < num_pages; i++)
  428. mem->pages[i] = pages[i];
  429. mem->page_count = num_pages;
  430. mem->is_flushed = true;
  431. ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
  432. if (ret != 0) {
  433. DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
  434. agp_free_memory(mem);
  435. return NULL;
  436. }
  437. return mem;
  438. }
  439. EXPORT_SYMBOL(drm_agp_bind_pages);