drm_pci.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* drm_pci.h -- PCI DMA memory management wrappers for DRM -*- linux-c -*- */
  2. /**
  3. * \file drm_pci.c
  4. * \brief Functions and ioctls to manage PCI memory
  5. *
  6. * \warning These interfaces aren't stable yet.
  7. *
  8. * \todo Implement the remaining ioctl's for the PCI pools.
  9. * \todo The wrappers here are so thin that they would be better off inlined..
  10. *
  11. * \author José Fonseca <jrfonseca@tungstengraphics.com>
  12. * \author Leif Delgass <ldelgass@retinalburn.net>
  13. */
  14. /*
  15. * Copyright 2003 José Fonseca.
  16. * Copyright 2003 Leif Delgass.
  17. * All Rights Reserved.
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a
  20. * copy of this software and associated documentation files (the "Software"),
  21. * to deal in the Software without restriction, including without limitation
  22. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  23. * and/or sell copies of the Software, and to permit persons to whom the
  24. * Software is furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice (including the next
  27. * paragraph) shall be included in all copies or substantial portions of the
  28. * Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  33. * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  34. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  35. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. */
  37. #include <linux/pci.h>
  38. #include <linux/slab.h>
  39. #include <linux/dma-mapping.h>
  40. #include "drmP.h"
  41. /**********************************************************************/
  42. /** \name PCI memory */
  43. /*@{*/
  44. /**
  45. * \brief Allocate a PCI consistent memory block, for DMA.
  46. */
  47. drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size, size_t align)
  48. {
  49. drm_dma_handle_t *dmah;
  50. #if 1
  51. unsigned long addr;
  52. size_t sz;
  53. #endif
  54. /* pci_alloc_consistent only guarantees alignment to the smallest
  55. * PAGE_SIZE order which is greater than or equal to the requested size.
  56. * Return NULL here for now to make sure nobody tries for larger alignment
  57. */
  58. if (align > size)
  59. return NULL;
  60. dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL);
  61. if (!dmah)
  62. return NULL;
  63. dmah->size = size;
  64. dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size, &dmah->busaddr, GFP_KERNEL | __GFP_COMP);
  65. if (dmah->vaddr == NULL) {
  66. kfree(dmah);
  67. return NULL;
  68. }
  69. memset(dmah->vaddr, 0, size);
  70. /* XXX - Is virt_to_page() legal for consistent mem? */
  71. /* Reserve */
  72. for (addr = (unsigned long)dmah->vaddr, sz = size;
  73. sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
  74. SetPageReserved(virt_to_page(addr));
  75. }
  76. return dmah;
  77. }
  78. EXPORT_SYMBOL(drm_pci_alloc);
  79. /**
  80. * \brief Free a PCI consistent memory block without freeing its descriptor.
  81. *
  82. * This function is for internal use in the Linux-specific DRM core code.
  83. */
  84. void __drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
  85. {
  86. #if 1
  87. unsigned long addr;
  88. size_t sz;
  89. #endif
  90. if (dmah->vaddr) {
  91. /* XXX - Is virt_to_page() legal for consistent mem? */
  92. /* Unreserve */
  93. for (addr = (unsigned long)dmah->vaddr, sz = dmah->size;
  94. sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
  95. ClearPageReserved(virt_to_page(addr));
  96. }
  97. dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr,
  98. dmah->busaddr);
  99. }
  100. }
  101. /**
  102. * \brief Free a PCI consistent memory block
  103. */
  104. void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
  105. {
  106. __drm_pci_free(dev, dmah);
  107. kfree(dmah);
  108. }
  109. EXPORT_SYMBOL(drm_pci_free);
  110. #ifdef CONFIG_PCI
  111. /**
  112. * Register.
  113. *
  114. * \param pdev - PCI device structure
  115. * \param ent entry from the PCI ID table with device type flags
  116. * \return zero on success or a negative number on failure.
  117. *
  118. * Attempt to gets inter module "drm" information. If we are first
  119. * then register the character device and inter module information.
  120. * Try and register, if we fail to register, backout previous work.
  121. */
  122. int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  123. struct drm_driver *driver)
  124. {
  125. struct drm_device *dev;
  126. int ret;
  127. DRM_DEBUG("\n");
  128. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  129. if (!dev)
  130. return -ENOMEM;
  131. ret = pci_enable_device(pdev);
  132. if (ret)
  133. goto err_g1;
  134. pci_set_master(pdev);
  135. dev->pdev = pdev;
  136. dev->dev = &pdev->dev;
  137. dev->pci_device = pdev->device;
  138. dev->pci_vendor = pdev->vendor;
  139. #ifdef __alpha__
  140. dev->hose = pdev->sysdata;
  141. #endif
  142. mutex_lock(&drm_global_mutex);
  143. if ((ret = drm_fill_in_dev(dev, ent, driver))) {
  144. printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  145. goto err_g2;
  146. }
  147. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  148. pci_set_drvdata(pdev, dev);
  149. ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
  150. if (ret)
  151. goto err_g2;
  152. }
  153. if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
  154. goto err_g3;
  155. if (dev->driver->load) {
  156. ret = dev->driver->load(dev, ent->driver_data);
  157. if (ret)
  158. goto err_g4;
  159. }
  160. /* setup the grouping for the legacy output */
  161. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  162. ret = drm_mode_group_init_legacy_group(dev,
  163. &dev->primary->mode_group);
  164. if (ret)
  165. goto err_g4;
  166. }
  167. list_add_tail(&dev->driver_item, &driver->device_list);
  168. DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
  169. driver->name, driver->major, driver->minor, driver->patchlevel,
  170. driver->date, pci_name(pdev), dev->primary->index);
  171. mutex_unlock(&drm_global_mutex);
  172. return 0;
  173. err_g4:
  174. drm_put_minor(&dev->primary);
  175. err_g3:
  176. if (drm_core_check_feature(dev, DRIVER_MODESET))
  177. drm_put_minor(&dev->control);
  178. err_g2:
  179. pci_disable_device(pdev);
  180. err_g1:
  181. kfree(dev);
  182. mutex_unlock(&drm_global_mutex);
  183. return ret;
  184. }
  185. EXPORT_SYMBOL(drm_get_pci_dev);
  186. /**
  187. * PCI device initialization. Called via drm_init at module load time,
  188. *
  189. * \return zero on success or a negative number on failure.
  190. *
  191. * Initializes a drm_device structures,registering the
  192. * stubs and initializing the AGP device.
  193. *
  194. * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and
  195. * after the initialization for driver customization.
  196. */
  197. int drm_pci_init(struct drm_driver *driver)
  198. {
  199. struct pci_dev *pdev = NULL;
  200. const struct pci_device_id *pid;
  201. int i;
  202. if (driver->driver_features & DRIVER_MODESET)
  203. return pci_register_driver(&driver->pci_driver);
  204. /* If not using KMS, fall back to stealth mode manual scanning. */
  205. for (i = 0; driver->pci_driver.id_table[i].vendor != 0; i++) {
  206. pid = &driver->pci_driver.id_table[i];
  207. /* Loop around setting up a DRM device for each PCI device
  208. * matching our ID and device class. If we had the internal
  209. * function that pci_get_subsys and pci_get_class used, we'd
  210. * be able to just pass pid in instead of doing a two-stage
  211. * thing.
  212. */
  213. pdev = NULL;
  214. while ((pdev =
  215. pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
  216. pid->subdevice, pdev)) != NULL) {
  217. if ((pdev->class & pid->class_mask) != pid->class)
  218. continue;
  219. /* stealth mode requires a manual probe */
  220. pci_dev_get(pdev);
  221. drm_get_pci_dev(pdev, pid, driver);
  222. }
  223. }
  224. return 0;
  225. }
  226. #else
  227. int drm_pci_init(struct drm_driver *driver)
  228. {
  229. return -1;
  230. }
  231. #endif
  232. /*@}*/