drm_pci.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2003 José Fonseca.
  3. * Copyright 2003 Leif Delgass.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <linux/dma-mapping.h>
  25. #include <linux/export.h>
  26. #include <linux/list.h>
  27. #include <linux/mutex.h>
  28. #include <linux/pci.h>
  29. #include <linux/slab.h>
  30. #include <drm/drm.h>
  31. #include <drm/drm_drv.h>
  32. #include <drm/drm_print.h>
  33. #include "drm_internal.h"
  34. #include "drm_legacy.h"
  35. #ifdef CONFIG_DRM_LEGACY
  36. /* List of devices hanging off drivers with stealth attach. */
  37. static LIST_HEAD(legacy_dev_list);
  38. static DEFINE_MUTEX(legacy_dev_list_lock);
  39. #endif
  40. static int drm_get_pci_domain(struct drm_device *dev)
  41. {
  42. #ifndef __alpha__
  43. /* For historical reasons, drm_get_pci_domain() is busticated
  44. * on most archs and has to remain so for userspace interface
  45. * < 1.4, except on alpha which was right from the beginning
  46. */
  47. if (dev->if_version < 0x10004)
  48. return 0;
  49. #endif /* __alpha__ */
  50. return pci_domain_nr(to_pci_dev(dev->dev)->bus);
  51. }
  52. int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
  53. {
  54. struct pci_dev *pdev = to_pci_dev(dev->dev);
  55. master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
  56. drm_get_pci_domain(dev),
  57. pdev->bus->number,
  58. PCI_SLOT(pdev->devfn),
  59. PCI_FUNC(pdev->devfn));
  60. if (!master->unique)
  61. return -ENOMEM;
  62. master->unique_len = strlen(master->unique);
  63. return 0;
  64. }
  65. #ifdef CONFIG_DRM_LEGACY
  66. static int drm_legacy_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
  67. {
  68. struct pci_dev *pdev = to_pci_dev(dev->dev);
  69. if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
  70. (p->busnum & 0xff) != pdev->bus->number ||
  71. p->devnum != PCI_SLOT(pdev->devfn) || p->funcnum != PCI_FUNC(pdev->devfn))
  72. return -EINVAL;
  73. p->irq = pdev->irq;
  74. DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
  75. p->irq);
  76. return 0;
  77. }
  78. /**
  79. * drm_legacy_irq_by_busid - Get interrupt from bus ID
  80. * @dev: DRM device
  81. * @data: IOCTL parameter pointing to a drm_irq_busid structure
  82. * @file_priv: DRM file private.
  83. *
  84. * Finds the PCI device with the specified bus id and gets its IRQ number.
  85. * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
  86. * to that of the device that this DRM instance attached to.
  87. *
  88. * Return: 0 on success or a negative error code on failure.
  89. */
  90. int drm_legacy_irq_by_busid(struct drm_device *dev, void *data,
  91. struct drm_file *file_priv)
  92. {
  93. struct drm_irq_busid *p = data;
  94. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  95. return -EOPNOTSUPP;
  96. /* UMS was only ever support on PCI devices. */
  97. if (WARN_ON(!dev_is_pci(dev->dev)))
  98. return -EINVAL;
  99. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  100. return -EOPNOTSUPP;
  101. return drm_legacy_pci_irq_by_busid(dev, p);
  102. }
  103. void drm_legacy_pci_agp_destroy(struct drm_device *dev)
  104. {
  105. if (dev->agp) {
  106. arch_phys_wc_del(dev->agp->agp_mtrr);
  107. drm_legacy_agp_clear(dev);
  108. kfree(dev->agp);
  109. dev->agp = NULL;
  110. }
  111. }
  112. static void drm_legacy_pci_agp_init(struct drm_device *dev)
  113. {
  114. if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
  115. if (pci_find_capability(to_pci_dev(dev->dev), PCI_CAP_ID_AGP))
  116. dev->agp = drm_legacy_agp_init(dev);
  117. if (dev->agp) {
  118. dev->agp->agp_mtrr = arch_phys_wc_add(
  119. dev->agp->agp_info.aper_base,
  120. dev->agp->agp_info.aper_size *
  121. 1024 * 1024);
  122. }
  123. }
  124. }
  125. static int drm_legacy_get_pci_dev(struct pci_dev *pdev,
  126. const struct pci_device_id *ent,
  127. const struct drm_driver *driver)
  128. {
  129. struct drm_device *dev;
  130. int ret;
  131. DRM_DEBUG("\n");
  132. dev = drm_dev_alloc(driver, &pdev->dev);
  133. if (IS_ERR(dev))
  134. return PTR_ERR(dev);
  135. ret = pci_enable_device(pdev);
  136. if (ret)
  137. goto err_free;
  138. #ifdef __alpha__
  139. dev->hose = pdev->sysdata;
  140. #endif
  141. drm_legacy_pci_agp_init(dev);
  142. ret = drm_dev_register(dev, ent->driver_data);
  143. if (ret)
  144. goto err_agp;
  145. if (drm_core_check_feature(dev, DRIVER_LEGACY)) {
  146. mutex_lock(&legacy_dev_list_lock);
  147. list_add_tail(&dev->legacy_dev_list, &legacy_dev_list);
  148. mutex_unlock(&legacy_dev_list_lock);
  149. }
  150. return 0;
  151. err_agp:
  152. drm_legacy_pci_agp_destroy(dev);
  153. pci_disable_device(pdev);
  154. err_free:
  155. drm_dev_put(dev);
  156. return ret;
  157. }
  158. /**
  159. * drm_legacy_pci_init - shadow-attach a legacy DRM PCI driver
  160. * @driver: DRM device driver
  161. * @pdriver: PCI device driver
  162. *
  163. * This is only used by legacy dri1 drivers and deprecated.
  164. *
  165. * Return: 0 on success or a negative error code on failure.
  166. */
  167. int drm_legacy_pci_init(const struct drm_driver *driver,
  168. struct pci_driver *pdriver)
  169. {
  170. struct pci_dev *pdev = NULL;
  171. const struct pci_device_id *pid;
  172. int i;
  173. DRM_DEBUG("\n");
  174. if (WARN_ON(!(driver->driver_features & DRIVER_LEGACY)))
  175. return -EINVAL;
  176. /* If not using KMS, fall back to stealth mode manual scanning. */
  177. for (i = 0; pdriver->id_table[i].vendor != 0; i++) {
  178. pid = &pdriver->id_table[i];
  179. /* Loop around setting up a DRM device for each PCI device
  180. * matching our ID and device class. If we had the internal
  181. * function that pci_get_subsys and pci_get_class used, we'd
  182. * be able to just pass pid in instead of doing a two-stage
  183. * thing.
  184. */
  185. pdev = NULL;
  186. while ((pdev =
  187. pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
  188. pid->subdevice, pdev)) != NULL) {
  189. if ((pdev->class & pid->class_mask) != pid->class)
  190. continue;
  191. /* stealth mode requires a manual probe */
  192. pci_dev_get(pdev);
  193. drm_legacy_get_pci_dev(pdev, pid, driver);
  194. }
  195. }
  196. return 0;
  197. }
  198. EXPORT_SYMBOL(drm_legacy_pci_init);
  199. /**
  200. * drm_legacy_pci_exit - unregister shadow-attach legacy DRM driver
  201. * @driver: DRM device driver
  202. * @pdriver: PCI device driver
  203. *
  204. * Unregister a DRM driver shadow-attached through drm_legacy_pci_init(). This
  205. * is deprecated and only used by dri1 drivers.
  206. */
  207. void drm_legacy_pci_exit(const struct drm_driver *driver,
  208. struct pci_driver *pdriver)
  209. {
  210. struct drm_device *dev, *tmp;
  211. DRM_DEBUG("\n");
  212. if (!(driver->driver_features & DRIVER_LEGACY)) {
  213. WARN_ON(1);
  214. } else {
  215. mutex_lock(&legacy_dev_list_lock);
  216. list_for_each_entry_safe(dev, tmp, &legacy_dev_list,
  217. legacy_dev_list) {
  218. if (dev->driver == driver) {
  219. list_del(&dev->legacy_dev_list);
  220. drm_put_dev(dev);
  221. }
  222. }
  223. mutex_unlock(&legacy_dev_list_lock);
  224. }
  225. DRM_INFO("Module unloaded\n");
  226. }
  227. EXPORT_SYMBOL(drm_legacy_pci_exit);
  228. #endif