drm_agpsupport.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. dev->agp->bridge = agp_backend_acquire(dev->pdev);
  93. if (!dev->agp->bridge)
  94. return -ENODEV;
  95. dev->agp->acquired = 1;
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(drm_agp_acquire);
  99. /**
  100. * Acquire the AGP device (ioctl).
  101. *
  102. * \param inode device inode.
  103. * \param file_priv DRM file private.
  104. * \param cmd command.
  105. * \param arg user argument.
  106. * \return zero on success or a negative number on failure.
  107. *
  108. * Verifies the AGP device hasn't been acquired before and calls
  109. * \c agp_backend_acquire.
  110. */
  111. int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
  112. struct drm_file *file_priv)
  113. {
  114. return drm_agp_acquire((struct drm_device *) file_priv->minor->dev);
  115. }
  116. /**
  117. * Release the AGP device.
  118. *
  119. * \param dev DRM device that is to release AGP.
  120. * \return zero on success or a negative number on failure.
  121. *
  122. * Verifies the AGP device has been acquired and calls \c agp_backend_release.
  123. */
  124. int drm_agp_release(struct drm_device *dev)
  125. {
  126. if (!dev->agp || !dev->agp->acquired)
  127. return -EINVAL;
  128. agp_backend_release(dev->agp->bridge);
  129. dev->agp->acquired = 0;
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(drm_agp_release);
  133. int drm_agp_release_ioctl(struct drm_device *dev, void *data,
  134. struct drm_file *file_priv)
  135. {
  136. return drm_agp_release(dev);
  137. }
  138. /**
  139. * Enable the AGP bus.
  140. *
  141. * \param dev DRM device that has previously acquired AGP.
  142. * \param mode Requested AGP mode.
  143. * \return zero on success or a negative number on failure.
  144. *
  145. * Verifies the AGP device has been acquired but not enabled, and calls
  146. * \c agp_enable.
  147. */
  148. int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
  149. {
  150. if (!dev->agp || !dev->agp->acquired)
  151. return -EINVAL;
  152. dev->agp->mode = mode.mode;
  153. agp_enable(dev->agp->bridge, mode.mode);
  154. dev->agp->enabled = 1;
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(drm_agp_enable);
  158. int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
  159. struct drm_file *file_priv)
  160. {
  161. struct drm_agp_mode *mode = data;
  162. return drm_agp_enable(dev, *mode);
  163. }
  164. /**
  165. * Allocate AGP memory.
  166. *
  167. * \param inode device inode.
  168. * \param file_priv file private pointer.
  169. * \param cmd command.
  170. * \param arg pointer to a drm_agp_buffer structure.
  171. * \return zero on success or a negative number on failure.
  172. *
  173. * Verifies the AGP device is present and has been acquired, allocates the
  174. * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it.
  175. */
  176. int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
  177. {
  178. struct drm_agp_mem *entry;
  179. struct agp_memory *memory;
  180. unsigned long pages;
  181. u32 type;
  182. if (!dev->agp || !dev->agp->acquired)
  183. return -EINVAL;
  184. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  185. if (!entry)
  186. return -ENOMEM;
  187. pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
  188. type = (u32) request->type;
  189. memory = agp_allocate_memory(dev->agp->bridge, pages, type);
  190. if (!memory) {
  191. kfree(entry);
  192. return -ENOMEM;
  193. }
  194. entry->handle = (unsigned long)memory->key + 1;
  195. entry->memory = memory;
  196. entry->bound = 0;
  197. entry->pages = pages;
  198. list_add(&entry->head, &dev->agp->memory);
  199. request->handle = entry->handle;
  200. request->physical = memory->physical;
  201. return 0;
  202. }
  203. EXPORT_SYMBOL(drm_agp_alloc);
  204. int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
  205. struct drm_file *file_priv)
  206. {
  207. struct drm_agp_buffer *request = data;
  208. return drm_agp_alloc(dev, request);
  209. }
  210. /**
  211. * Search for the AGP memory entry associated with a handle.
  212. *
  213. * \param dev DRM device structure.
  214. * \param handle AGP memory handle.
  215. * \return pointer to the drm_agp_mem structure associated with \p handle.
  216. *
  217. * Walks through drm_agp_head::memory until finding a matching handle.
  218. */
  219. static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device *dev,
  220. unsigned long handle)
  221. {
  222. struct drm_agp_mem *entry;
  223. list_for_each_entry(entry, &dev->agp->memory, head) {
  224. if (entry->handle == handle)
  225. return entry;
  226. }
  227. return NULL;
  228. }
  229. /**
  230. * Unbind AGP memory from the GATT (ioctl).
  231. *
  232. * \param inode device inode.
  233. * \param file_priv DRM file private.
  234. * \param cmd command.
  235. * \param arg pointer to a drm_agp_binding structure.
  236. * \return zero on success or a negative number on failure.
  237. *
  238. * Verifies the AGP device is present and acquired, looks-up the AGP memory
  239. * entry and passes it to the unbind_agp() function.
  240. */
  241. int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
  242. {
  243. struct drm_agp_mem *entry;
  244. int ret;
  245. if (!dev->agp || !dev->agp->acquired)
  246. return -EINVAL;
  247. entry = drm_agp_lookup_entry(dev, request->handle);
  248. if (!entry || !entry->bound)
  249. return -EINVAL;
  250. ret = drm_unbind_agp(entry->memory);
  251. if (ret == 0)
  252. entry->bound = 0;
  253. return ret;
  254. }
  255. EXPORT_SYMBOL(drm_agp_unbind);
  256. int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
  257. struct drm_file *file_priv)
  258. {
  259. struct drm_agp_binding *request = data;
  260. return drm_agp_unbind(dev, request);
  261. }
  262. /**
  263. * Bind AGP memory into the GATT (ioctl)
  264. *
  265. * \param inode device inode.
  266. * \param file_priv DRM file private.
  267. * \param cmd command.
  268. * \param arg pointer to a drm_agp_binding structure.
  269. * \return zero on success or a negative number on failure.
  270. *
  271. * Verifies the AGP device is present and has been acquired and that no memory
  272. * is currently bound into the GATT. Looks-up the AGP memory entry and passes
  273. * it to bind_agp() function.
  274. */
  275. int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
  276. {
  277. struct drm_agp_mem *entry;
  278. int retcode;
  279. int page;
  280. if (!dev->agp || !dev->agp->acquired)
  281. return -EINVAL;
  282. entry = drm_agp_lookup_entry(dev, request->handle);
  283. if (!entry || entry->bound)
  284. return -EINVAL;
  285. page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
  286. retcode = drm_bind_agp(entry->memory, page);
  287. if (retcode)
  288. return retcode;
  289. entry->bound = dev->agp->base + (page << PAGE_SHIFT);
  290. DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
  291. dev->agp->base, entry->bound);
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(drm_agp_bind);
  295. int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
  296. struct drm_file *file_priv)
  297. {
  298. struct drm_agp_binding *request = data;
  299. return drm_agp_bind(dev, request);
  300. }
  301. /**
  302. * Free AGP memory (ioctl).
  303. *
  304. * \param inode device inode.
  305. * \param file_priv DRM file private.
  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 drm_device *dev, struct drm_agp_buffer *request)
  316. {
  317. struct drm_agp_mem *entry;
  318. if (!dev->agp || !dev->agp->acquired)
  319. return -EINVAL;
  320. entry = drm_agp_lookup_entry(dev, request->handle);
  321. if (!entry)
  322. return -EINVAL;
  323. if (entry->bound)
  324. drm_unbind_agp(entry->memory);
  325. list_del(&entry->head);
  326. drm_free_agp(entry->memory, entry->pages);
  327. kfree(entry);
  328. return 0;
  329. }
  330. EXPORT_SYMBOL(drm_agp_free);
  331. int drm_agp_free_ioctl(struct drm_device *dev, void *data,
  332. struct drm_file *file_priv)
  333. {
  334. struct drm_agp_buffer *request = data;
  335. return drm_agp_free(dev, request);
  336. }
  337. /**
  338. * Initialize the AGP resources.
  339. *
  340. * \return pointer to a drm_agp_head structure.
  341. *
  342. * Gets the drm_agp_t structure which is made available by the agpgart module
  343. * via the inter_module_* functions. Creates and initializes a drm_agp_head
  344. * structure.
  345. *
  346. * Note that final cleanup of the kmalloced structure is directly done in
  347. * drm_pci_agp_destroy.
  348. */
  349. struct drm_agp_head *drm_agp_init(struct drm_device *dev)
  350. {
  351. struct drm_agp_head *head = NULL;
  352. head = kzalloc(sizeof(*head), GFP_KERNEL);
  353. if (!head)
  354. return NULL;
  355. head->bridge = agp_find_bridge(dev->pdev);
  356. if (!head->bridge) {
  357. head->bridge = agp_backend_acquire(dev->pdev);
  358. if (!head->bridge) {
  359. kfree(head);
  360. return NULL;
  361. }
  362. agp_copy_info(head->bridge, &head->agp_info);
  363. agp_backend_release(head->bridge);
  364. } else {
  365. agp_copy_info(head->bridge, &head->agp_info);
  366. }
  367. if (head->agp_info.chipset == NOT_SUPPORTED) {
  368. kfree(head);
  369. return NULL;
  370. }
  371. INIT_LIST_HEAD(&head->memory);
  372. head->cant_use_aperture = head->agp_info.cant_use_aperture;
  373. head->page_mask = head->agp_info.page_mask;
  374. head->base = head->agp_info.aper_base;
  375. return head;
  376. }
  377. /* Only exported for i810.ko */
  378. EXPORT_SYMBOL(drm_agp_init);
  379. /**
  380. * drm_legacy_agp_clear - Clear AGP resource list
  381. * @dev: DRM device
  382. *
  383. * Iterate over all AGP resources and remove them. But keep the AGP head
  384. * intact so it can still be used. It is safe to call this if AGP is disabled or
  385. * was already removed.
  386. *
  387. * Cleanup is only done for drivers who have DRIVER_LEGACY set.
  388. */
  389. void drm_legacy_agp_clear(struct drm_device *dev)
  390. {
  391. struct drm_agp_mem *entry, *tempe;
  392. if (!dev->agp)
  393. return;
  394. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  395. return;
  396. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  397. if (entry->bound)
  398. drm_unbind_agp(entry->memory);
  399. drm_free_agp(entry->memory, entry->pages);
  400. kfree(entry);
  401. }
  402. INIT_LIST_HEAD(&dev->agp->memory);
  403. if (dev->agp->acquired)
  404. drm_agp_release(dev);
  405. dev->agp->acquired = 0;
  406. dev->agp->enabled = 0;
  407. }
  408. /**
  409. * Binds a collection of pages into AGP memory at the given offset, returning
  410. * the AGP memory structure containing them.
  411. *
  412. * No reference is held on the pages during this time -- it is up to the
  413. * caller to handle that.
  414. */
  415. struct agp_memory *
  416. drm_agp_bind_pages(struct drm_device *dev,
  417. struct page **pages,
  418. unsigned long num_pages,
  419. uint32_t gtt_offset,
  420. u32 type)
  421. {
  422. struct agp_memory *mem;
  423. int ret, i;
  424. DRM_DEBUG("\n");
  425. mem = agp_allocate_memory(dev->agp->bridge, num_pages,
  426. type);
  427. if (mem == NULL) {
  428. DRM_ERROR("Failed to allocate memory for %ld pages\n",
  429. num_pages);
  430. return NULL;
  431. }
  432. for (i = 0; i < num_pages; i++)
  433. mem->pages[i] = pages[i];
  434. mem->page_count = num_pages;
  435. mem->is_flushed = true;
  436. ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
  437. if (ret != 0) {
  438. DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
  439. agp_free_memory(mem);
  440. return NULL;
  441. }
  442. return mem;
  443. }
  444. EXPORT_SYMBOL(drm_agp_bind_pages);