drm_agpsupport.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #if __OS_HAS_AGP
  37. #include <asm/agp.h>
  38. /**
  39. * Get AGP information.
  40. *
  41. * \param inode device inode.
  42. * \param file_priv DRM file private.
  43. * \param cmd command.
  44. * \param arg pointer to a (output) drm_agp_info structure.
  45. * \return zero on success or a negative number on failure.
  46. *
  47. * Verifies the AGP device has been initialized and acquired and fills in the
  48. * drm_agp_info structure with the information in drm_agp_head::agp_info.
  49. */
  50. int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
  51. {
  52. struct agp_kern_info *kern;
  53. if (!dev->agp || !dev->agp->acquired)
  54. return -EINVAL;
  55. kern = &dev->agp->agp_info;
  56. info->agp_version_major = kern->version.major;
  57. info->agp_version_minor = kern->version.minor;
  58. info->mode = kern->mode;
  59. info->aperture_base = kern->aper_base;
  60. info->aperture_size = kern->aper_size * 1024 * 1024;
  61. info->memory_allowed = kern->max_memory << PAGE_SHIFT;
  62. info->memory_used = kern->current_memory << PAGE_SHIFT;
  63. info->id_vendor = kern->device->vendor;
  64. info->id_device = kern->device->device;
  65. return 0;
  66. }
  67. EXPORT_SYMBOL(drm_agp_info);
  68. int drm_agp_info_ioctl(struct drm_device *dev, void *data,
  69. struct drm_file *file_priv)
  70. {
  71. struct drm_agp_info *info = data;
  72. int err;
  73. err = drm_agp_info(dev, info);
  74. if (err)
  75. return err;
  76. return 0;
  77. }
  78. /**
  79. * Acquire the AGP device.
  80. *
  81. * \param dev DRM device that is to acquire AGP.
  82. * \return zero on success or a negative number on failure.
  83. *
  84. * Verifies the AGP device hasn't been acquired before and calls
  85. * \c agp_backend_acquire.
  86. */
  87. int drm_agp_acquire(struct drm_device * dev)
  88. {
  89. if (!dev->agp)
  90. return -ENODEV;
  91. if (dev->agp->acquired)
  92. return -EBUSY;
  93. if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev)))
  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. if (!(entry = kzalloc(sizeof(*entry), GFP_KERNEL)))
  185. return -ENOMEM;
  186. pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
  187. type = (u32) request->type;
  188. if (!(memory = agp_allocate_memory(dev->agp->bridge, pages, type))) {
  189. kfree(entry);
  190. return -ENOMEM;
  191. }
  192. entry->handle = (unsigned long)memory->key + 1;
  193. entry->memory = memory;
  194. entry->bound = 0;
  195. entry->pages = pages;
  196. list_add(&entry->head, &dev->agp->memory);
  197. request->handle = entry->handle;
  198. request->physical = memory->physical;
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(drm_agp_alloc);
  202. int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
  203. struct drm_file *file_priv)
  204. {
  205. struct drm_agp_buffer *request = data;
  206. return drm_agp_alloc(dev, request);
  207. }
  208. /**
  209. * Search for the AGP memory entry associated with a handle.
  210. *
  211. * \param dev DRM device structure.
  212. * \param handle AGP memory handle.
  213. * \return pointer to the drm_agp_mem structure associated with \p handle.
  214. *
  215. * Walks through drm_agp_head::memory until finding a matching handle.
  216. */
  217. static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev,
  218. unsigned long handle)
  219. {
  220. struct drm_agp_mem *entry;
  221. list_for_each_entry(entry, &dev->agp->memory, head) {
  222. if (entry->handle == handle)
  223. return entry;
  224. }
  225. return NULL;
  226. }
  227. /**
  228. * Unbind AGP memory from the GATT (ioctl).
  229. *
  230. * \param inode device inode.
  231. * \param file_priv DRM file private.
  232. * \param cmd command.
  233. * \param arg pointer to a drm_agp_binding structure.
  234. * \return zero on success or a negative number on failure.
  235. *
  236. * Verifies the AGP device is present and acquired, looks-up the AGP memory
  237. * entry and passes it to the unbind_agp() function.
  238. */
  239. int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
  240. {
  241. struct drm_agp_mem *entry;
  242. int ret;
  243. if (!dev->agp || !dev->agp->acquired)
  244. return -EINVAL;
  245. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  246. return -EINVAL;
  247. if (!entry->bound)
  248. return -EINVAL;
  249. ret = drm_unbind_agp(entry->memory);
  250. if (ret == 0)
  251. entry->bound = 0;
  252. return ret;
  253. }
  254. EXPORT_SYMBOL(drm_agp_unbind);
  255. int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
  256. struct drm_file *file_priv)
  257. {
  258. struct drm_agp_binding *request = data;
  259. return drm_agp_unbind(dev, request);
  260. }
  261. /**
  262. * Bind AGP memory into the GATT (ioctl)
  263. *
  264. * \param inode device inode.
  265. * \param file_priv DRM file private.
  266. * \param cmd command.
  267. * \param arg pointer to a drm_agp_binding structure.
  268. * \return zero on success or a negative number on failure.
  269. *
  270. * Verifies the AGP device is present and has been acquired and that no memory
  271. * is currently bound into the GATT. Looks-up the AGP memory entry and passes
  272. * it to bind_agp() function.
  273. */
  274. int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
  275. {
  276. struct drm_agp_mem *entry;
  277. int retcode;
  278. int page;
  279. if (!dev->agp || !dev->agp->acquired)
  280. return -EINVAL;
  281. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  282. return -EINVAL;
  283. if (entry->bound)
  284. return -EINVAL;
  285. page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
  286. if ((retcode = drm_bind_agp(entry->memory, page)))
  287. return retcode;
  288. entry->bound = dev->agp->base + (page << PAGE_SHIFT);
  289. DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n",
  290. dev->agp->base, entry->bound);
  291. return 0;
  292. }
  293. EXPORT_SYMBOL(drm_agp_bind);
  294. int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
  295. struct drm_file *file_priv)
  296. {
  297. struct drm_agp_binding *request = data;
  298. return drm_agp_bind(dev, request);
  299. }
  300. /**
  301. * Free AGP memory (ioctl).
  302. *
  303. * \param inode device inode.
  304. * \param file_priv DRM file private.
  305. * \param cmd command.
  306. * \param arg pointer to a drm_agp_buffer structure.
  307. * \return zero on success or a negative number on failure.
  308. *
  309. * Verifies the AGP device is present and has been acquired and looks up the
  310. * AGP memory entry. If the memory it's currently bound, unbind it via
  311. * unbind_agp(). Frees it via free_agp() as well as the entry itself
  312. * and unlinks from the doubly linked list it's inserted in.
  313. */
  314. int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
  315. {
  316. struct drm_agp_mem *entry;
  317. if (!dev->agp || !dev->agp->acquired)
  318. return -EINVAL;
  319. if (!(entry = drm_agp_lookup_entry(dev, request->handle)))
  320. return -EINVAL;
  321. if (entry->bound)
  322. drm_unbind_agp(entry->memory);
  323. list_del(&entry->head);
  324. drm_free_agp(entry->memory, entry->pages);
  325. kfree(entry);
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(drm_agp_free);
  329. int drm_agp_free_ioctl(struct drm_device *dev, void *data,
  330. struct drm_file *file_priv)
  331. {
  332. struct drm_agp_buffer *request = data;
  333. return drm_agp_free(dev, request);
  334. }
  335. /**
  336. * Initialize the AGP resources.
  337. *
  338. * \return pointer to a drm_agp_head structure.
  339. *
  340. * Gets the drm_agp_t structure which is made available by the agpgart module
  341. * via the inter_module_* functions. Creates and initializes a drm_agp_head
  342. * structure.
  343. *
  344. * Note that final cleanup of the kmalloced structure is directly done in
  345. * drm_pci_agp_destroy.
  346. */
  347. struct drm_agp_head *drm_agp_init(struct drm_device *dev)
  348. {
  349. struct drm_agp_head *head = NULL;
  350. if (!(head = kzalloc(sizeof(*head), GFP_KERNEL)))
  351. return NULL;
  352. head->bridge = agp_find_bridge(dev->pdev);
  353. if (!head->bridge) {
  354. if (!(head->bridge = agp_backend_acquire(dev->pdev))) {
  355. kfree(head);
  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. kfree(head);
  365. return NULL;
  366. }
  367. INIT_LIST_HEAD(&head->memory);
  368. head->cant_use_aperture = head->agp_info.cant_use_aperture;
  369. head->page_mask = head->agp_info.page_mask;
  370. head->base = head->agp_info.aper_base;
  371. return head;
  372. }
  373. /**
  374. * drm_agp_clear - Clear AGP resource list
  375. * @dev: DRM device
  376. *
  377. * Iterate over all AGP resources and remove them. But keep the AGP head
  378. * intact so it can still be used. It is safe to call this if AGP is disabled or
  379. * was already removed.
  380. *
  381. * If DRIVER_MODESET is active, nothing is done to protect the modesetting
  382. * resources from getting destroyed. Drivers are responsible of cleaning them up
  383. * during device shutdown.
  384. */
  385. void drm_agp_clear(struct drm_device *dev)
  386. {
  387. struct drm_agp_mem *entry, *tempe;
  388. if (!dev->agp)
  389. return;
  390. if (drm_core_check_feature(dev, DRIVER_MODESET))
  391. return;
  392. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  393. if (entry->bound)
  394. drm_unbind_agp(entry->memory);
  395. drm_free_agp(entry->memory, entry->pages);
  396. kfree(entry);
  397. }
  398. INIT_LIST_HEAD(&dev->agp->memory);
  399. if (dev->agp->acquired)
  400. drm_agp_release(dev);
  401. dev->agp->acquired = 0;
  402. dev->agp->enabled = 0;
  403. }
  404. /**
  405. * Binds a collection of pages into AGP memory at the given offset, returning
  406. * the AGP memory structure containing them.
  407. *
  408. * No reference is held on the pages during this time -- it is up to the
  409. * caller to handle that.
  410. */
  411. struct agp_memory *
  412. drm_agp_bind_pages(struct drm_device *dev,
  413. struct page **pages,
  414. unsigned long num_pages,
  415. uint32_t gtt_offset,
  416. u32 type)
  417. {
  418. struct agp_memory *mem;
  419. int ret, i;
  420. DRM_DEBUG("\n");
  421. mem = agp_allocate_memory(dev->agp->bridge, num_pages,
  422. type);
  423. if (mem == NULL) {
  424. DRM_ERROR("Failed to allocate memory for %ld pages\n",
  425. num_pages);
  426. return NULL;
  427. }
  428. for (i = 0; i < num_pages; i++)
  429. mem->pages[i] = pages[i];
  430. mem->page_count = num_pages;
  431. mem->is_flushed = true;
  432. ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE);
  433. if (ret != 0) {
  434. DRM_ERROR("Failed to bind AGP memory: %d\n", ret);
  435. agp_free_memory(mem);
  436. return NULL;
  437. }
  438. return mem;
  439. }
  440. EXPORT_SYMBOL(drm_agp_bind_pages);
  441. #endif /* __OS_HAS_AGP */