drm_pci.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. if ((ret = drm_fill_in_dev(dev, ent, driver))) {
  143. printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  144. goto err_g2;
  145. }
  146. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  147. pci_set_drvdata(pdev, dev);
  148. ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
  149. if (ret)
  150. goto err_g2;
  151. }
  152. if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
  153. goto err_g3;
  154. if (dev->driver->load) {
  155. ret = dev->driver->load(dev, ent->driver_data);
  156. if (ret)
  157. goto err_g4;
  158. }
  159. /* setup the grouping for the legacy output */
  160. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  161. ret = drm_mode_group_init_legacy_group(dev,
  162. &dev->primary->mode_group);
  163. if (ret)
  164. goto err_g4;
  165. }
  166. list_add_tail(&dev->driver_item, &driver->device_list);
  167. DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
  168. driver->name, driver->major, driver->minor, driver->patchlevel,
  169. driver->date, pci_name(pdev), dev->primary->index);
  170. return 0;
  171. err_g4:
  172. drm_put_minor(&dev->primary);
  173. err_g3:
  174. if (drm_core_check_feature(dev, DRIVER_MODESET))
  175. drm_put_minor(&dev->control);
  176. err_g2:
  177. pci_disable_device(pdev);
  178. err_g1:
  179. kfree(dev);
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(drm_get_pci_dev);
  183. /**
  184. * PCI device initialization. Called via drm_init at module load time,
  185. *
  186. * \return zero on success or a negative number on failure.
  187. *
  188. * Initializes a drm_device structures,registering the
  189. * stubs and initializing the AGP device.
  190. *
  191. * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and
  192. * after the initialization for driver customization.
  193. */
  194. int drm_pci_init(struct drm_driver *driver)
  195. {
  196. struct pci_dev *pdev = NULL;
  197. const struct pci_device_id *pid;
  198. int i;
  199. if (driver->driver_features & DRIVER_MODESET)
  200. return pci_register_driver(&driver->pci_driver);
  201. /* If not using KMS, fall back to stealth mode manual scanning. */
  202. for (i = 0; driver->pci_driver.id_table[i].vendor != 0; i++) {
  203. pid = &driver->pci_driver.id_table[i];
  204. /* Loop around setting up a DRM device for each PCI device
  205. * matching our ID and device class. If we had the internal
  206. * function that pci_get_subsys and pci_get_class used, we'd
  207. * be able to just pass pid in instead of doing a two-stage
  208. * thing.
  209. */
  210. pdev = NULL;
  211. while ((pdev =
  212. pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
  213. pid->subdevice, pdev)) != NULL) {
  214. if ((pdev->class & pid->class_mask) != pid->class)
  215. continue;
  216. /* stealth mode requires a manual probe */
  217. pci_dev_get(pdev);
  218. drm_get_pci_dev(pdev, pid, driver);
  219. }
  220. }
  221. return 0;
  222. }
  223. #else
  224. int drm_pci_init(struct drm_driver *driver)
  225. {
  226. return -1;
  227. }
  228. #endif
  229. /*@}*/