drm_pci.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/pci.h>
  27. #include <linux/slab.h>
  28. #include <drm/drm.h>
  29. #include <drm/drm_agpsupport.h>
  30. #include <drm/drm_drv.h>
  31. #include <drm/drm_print.h>
  32. #include "drm_internal.h"
  33. #include "drm_legacy.h"
  34. #ifdef CONFIG_DRM_LEGACY
  35. /**
  36. * drm_pci_alloc - Allocate a PCI consistent memory block, for DMA.
  37. * @dev: DRM device
  38. * @size: size of block to allocate
  39. * @align: alignment of block
  40. *
  41. * FIXME: This is a needless abstraction of the Linux dma-api and should be
  42. * removed.
  43. *
  44. * Return: A handle to the allocated memory block on success or NULL on
  45. * failure.
  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. /* pci_alloc_consistent only guarantees alignment to the smallest
  51. * PAGE_SIZE order which is greater than or equal to the requested size.
  52. * Return NULL here for now to make sure nobody tries for larger alignment
  53. */
  54. if (align > size)
  55. return NULL;
  56. dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL);
  57. if (!dmah)
  58. return NULL;
  59. dmah->size = size;
  60. dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size,
  61. &dmah->busaddr,
  62. GFP_KERNEL);
  63. if (dmah->vaddr == NULL) {
  64. kfree(dmah);
  65. return NULL;
  66. }
  67. return dmah;
  68. }
  69. EXPORT_SYMBOL(drm_pci_alloc);
  70. /**
  71. * drm_pci_free - Free a PCI consistent memory block
  72. * @dev: DRM device
  73. * @dmah: handle to memory block
  74. *
  75. * FIXME: This is a needless abstraction of the Linux dma-api and should be
  76. * removed.
  77. */
  78. void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
  79. {
  80. dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr,
  81. dmah->busaddr);
  82. kfree(dmah);
  83. }
  84. EXPORT_SYMBOL(drm_pci_free);
  85. #endif
  86. static int drm_get_pci_domain(struct drm_device *dev)
  87. {
  88. #ifndef __alpha__
  89. /* For historical reasons, drm_get_pci_domain() is busticated
  90. * on most archs and has to remain so for userspace interface
  91. * < 1.4, except on alpha which was right from the beginning
  92. */
  93. if (dev->if_version < 0x10004)
  94. return 0;
  95. #endif /* __alpha__ */
  96. return pci_domain_nr(dev->pdev->bus);
  97. }
  98. int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
  99. {
  100. master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
  101. drm_get_pci_domain(dev),
  102. dev->pdev->bus->number,
  103. PCI_SLOT(dev->pdev->devfn),
  104. PCI_FUNC(dev->pdev->devfn));
  105. if (!master->unique)
  106. return -ENOMEM;
  107. master->unique_len = strlen(master->unique);
  108. return 0;
  109. }
  110. static int drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
  111. {
  112. if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
  113. (p->busnum & 0xff) != dev->pdev->bus->number ||
  114. p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
  115. return -EINVAL;
  116. p->irq = dev->pdev->irq;
  117. DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
  118. p->irq);
  119. return 0;
  120. }
  121. /**
  122. * drm_irq_by_busid - Get interrupt from bus ID
  123. * @dev: DRM device
  124. * @data: IOCTL parameter pointing to a drm_irq_busid structure
  125. * @file_priv: DRM file private.
  126. *
  127. * Finds the PCI device with the specified bus id and gets its IRQ number.
  128. * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
  129. * to that of the device that this DRM instance attached to.
  130. *
  131. * Return: 0 on success or a negative error code on failure.
  132. */
  133. int drm_irq_by_busid(struct drm_device *dev, void *data,
  134. struct drm_file *file_priv)
  135. {
  136. struct drm_irq_busid *p = data;
  137. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  138. return -EOPNOTSUPP;
  139. /* UMS was only ever support on PCI devices. */
  140. if (WARN_ON(!dev->pdev))
  141. return -EINVAL;
  142. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  143. return -EOPNOTSUPP;
  144. return drm_pci_irq_by_busid(dev, p);
  145. }
  146. void drm_pci_agp_destroy(struct drm_device *dev)
  147. {
  148. if (dev->agp) {
  149. arch_phys_wc_del(dev->agp->agp_mtrr);
  150. drm_legacy_agp_clear(dev);
  151. kfree(dev->agp);
  152. dev->agp = NULL;
  153. }
  154. }
  155. #ifdef CONFIG_DRM_LEGACY
  156. static void drm_pci_agp_init(struct drm_device *dev)
  157. {
  158. if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
  159. if (pci_find_capability(dev->pdev, PCI_CAP_ID_AGP))
  160. dev->agp = drm_agp_init(dev);
  161. if (dev->agp) {
  162. dev->agp->agp_mtrr = arch_phys_wc_add(
  163. dev->agp->agp_info.aper_base,
  164. dev->agp->agp_info.aper_size *
  165. 1024 * 1024);
  166. }
  167. }
  168. }
  169. static int drm_get_pci_dev(struct pci_dev *pdev,
  170. const struct pci_device_id *ent,
  171. struct drm_driver *driver)
  172. {
  173. struct drm_device *dev;
  174. int ret;
  175. DRM_DEBUG("\n");
  176. dev = drm_dev_alloc(driver, &pdev->dev);
  177. if (IS_ERR(dev))
  178. return PTR_ERR(dev);
  179. ret = pci_enable_device(pdev);
  180. if (ret)
  181. goto err_free;
  182. dev->pdev = pdev;
  183. #ifdef __alpha__
  184. dev->hose = pdev->sysdata;
  185. #endif
  186. if (drm_core_check_feature(dev, DRIVER_MODESET))
  187. pci_set_drvdata(pdev, dev);
  188. drm_pci_agp_init(dev);
  189. ret = drm_dev_register(dev, ent->driver_data);
  190. if (ret)
  191. goto err_agp;
  192. /* No locking needed since shadow-attach is single-threaded since it may
  193. * only be called from the per-driver module init hook. */
  194. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  195. list_add_tail(&dev->legacy_dev_list, &driver->legacy_dev_list);
  196. return 0;
  197. err_agp:
  198. drm_pci_agp_destroy(dev);
  199. pci_disable_device(pdev);
  200. err_free:
  201. drm_dev_put(dev);
  202. return ret;
  203. }
  204. /**
  205. * drm_legacy_pci_init - shadow-attach a legacy DRM PCI driver
  206. * @driver: DRM device driver
  207. * @pdriver: PCI device driver
  208. *
  209. * This is only used by legacy dri1 drivers and deprecated.
  210. *
  211. * Return: 0 on success or a negative error code on failure.
  212. */
  213. int drm_legacy_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
  214. {
  215. struct pci_dev *pdev = NULL;
  216. const struct pci_device_id *pid;
  217. int i;
  218. DRM_DEBUG("\n");
  219. if (WARN_ON(!(driver->driver_features & DRIVER_LEGACY)))
  220. return -EINVAL;
  221. /* If not using KMS, fall back to stealth mode manual scanning. */
  222. INIT_LIST_HEAD(&driver->legacy_dev_list);
  223. for (i = 0; pdriver->id_table[i].vendor != 0; i++) {
  224. pid = &pdriver->id_table[i];
  225. /* Loop around setting up a DRM device for each PCI device
  226. * matching our ID and device class. If we had the internal
  227. * function that pci_get_subsys and pci_get_class used, we'd
  228. * be able to just pass pid in instead of doing a two-stage
  229. * thing.
  230. */
  231. pdev = NULL;
  232. while ((pdev =
  233. pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
  234. pid->subdevice, pdev)) != NULL) {
  235. if ((pdev->class & pid->class_mask) != pid->class)
  236. continue;
  237. /* stealth mode requires a manual probe */
  238. pci_dev_get(pdev);
  239. drm_get_pci_dev(pdev, pid, driver);
  240. }
  241. }
  242. return 0;
  243. }
  244. EXPORT_SYMBOL(drm_legacy_pci_init);
  245. /**
  246. * drm_legacy_pci_exit - unregister shadow-attach legacy DRM driver
  247. * @driver: DRM device driver
  248. * @pdriver: PCI device driver
  249. *
  250. * Unregister a DRM driver shadow-attached through drm_legacy_pci_init(). This
  251. * is deprecated and only used by dri1 drivers.
  252. */
  253. void drm_legacy_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver)
  254. {
  255. struct drm_device *dev, *tmp;
  256. DRM_DEBUG("\n");
  257. if (!(driver->driver_features & DRIVER_LEGACY)) {
  258. WARN_ON(1);
  259. } else {
  260. list_for_each_entry_safe(dev, tmp, &driver->legacy_dev_list,
  261. legacy_dev_list) {
  262. list_del(&dev->legacy_dev_list);
  263. drm_put_dev(dev);
  264. }
  265. }
  266. DRM_INFO("Module unloaded\n");
  267. }
  268. EXPORT_SYMBOL(drm_legacy_pci_exit);
  269. #endif