drm_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/pci.h>
  25. #include <linux/slab.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/export.h>
  28. #include <drm/drm_pci.h>
  29. #include <drm/drmP.h>
  30. #include "drm_internal.h"
  31. #include "drm_legacy.h"
  32. /**
  33. * drm_pci_alloc - Allocate a PCI consistent memory block, for DMA.
  34. * @dev: DRM device
  35. * @size: size of block to allocate
  36. * @align: alignment of block
  37. *
  38. * FIXME: This is a needless abstraction of the Linux dma-api and should be
  39. * removed.
  40. *
  41. * Return: A handle to the allocated memory block on success or NULL on
  42. * failure.
  43. */
  44. drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size, size_t align)
  45. {
  46. drm_dma_handle_t *dmah;
  47. /* pci_alloc_consistent only guarantees alignment to the smallest
  48. * PAGE_SIZE order which is greater than or equal to the requested size.
  49. * Return NULL here for now to make sure nobody tries for larger alignment
  50. */
  51. if (align > size)
  52. return NULL;
  53. dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL);
  54. if (!dmah)
  55. return NULL;
  56. dmah->size = size;
  57. dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size, &dmah->busaddr, GFP_KERNEL);
  58. if (dmah->vaddr == NULL) {
  59. kfree(dmah);
  60. return NULL;
  61. }
  62. return dmah;
  63. }
  64. EXPORT_SYMBOL(drm_pci_alloc);
  65. /*
  66. * Free a PCI consistent memory block without freeing its descriptor.
  67. *
  68. * This function is for internal use in the Linux-specific DRM core code.
  69. */
  70. void __drm_legacy_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
  71. {
  72. if (dmah->vaddr)
  73. dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr,
  74. dmah->busaddr);
  75. }
  76. /**
  77. * drm_pci_free - Free a PCI consistent memory block
  78. * @dev: DRM device
  79. * @dmah: handle to memory block
  80. *
  81. * FIXME: This is a needless abstraction of the Linux dma-api and should be
  82. * removed.
  83. */
  84. void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
  85. {
  86. __drm_legacy_pci_free(dev, dmah);
  87. kfree(dmah);
  88. }
  89. EXPORT_SYMBOL(drm_pci_free);
  90. #ifdef CONFIG_PCI
  91. static int drm_get_pci_domain(struct drm_device *dev)
  92. {
  93. #ifndef __alpha__
  94. /* For historical reasons, drm_get_pci_domain() is busticated
  95. * on most archs and has to remain so for userspace interface
  96. * < 1.4, except on alpha which was right from the beginning
  97. */
  98. if (dev->if_version < 0x10004)
  99. return 0;
  100. #endif /* __alpha__ */
  101. return pci_domain_nr(dev->pdev->bus);
  102. }
  103. int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
  104. {
  105. master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
  106. drm_get_pci_domain(dev),
  107. dev->pdev->bus->number,
  108. PCI_SLOT(dev->pdev->devfn),
  109. PCI_FUNC(dev->pdev->devfn));
  110. if (!master->unique)
  111. return -ENOMEM;
  112. master->unique_len = strlen(master->unique);
  113. return 0;
  114. }
  115. static int drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
  116. {
  117. if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
  118. (p->busnum & 0xff) != dev->pdev->bus->number ||
  119. p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
  120. return -EINVAL;
  121. p->irq = dev->pdev->irq;
  122. DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
  123. p->irq);
  124. return 0;
  125. }
  126. /**
  127. * drm_irq_by_busid - Get interrupt from bus ID
  128. * @dev: DRM device
  129. * @data: IOCTL parameter pointing to a drm_irq_busid structure
  130. * @file_priv: DRM file private.
  131. *
  132. * Finds the PCI device with the specified bus id and gets its IRQ number.
  133. * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
  134. * to that of the device that this DRM instance attached to.
  135. *
  136. * Return: 0 on success or a negative error code on failure.
  137. */
  138. int drm_irq_by_busid(struct drm_device *dev, void *data,
  139. struct drm_file *file_priv)
  140. {
  141. struct drm_irq_busid *p = data;
  142. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  143. return -EINVAL;
  144. /* UMS was only ever support on PCI devices. */
  145. if (WARN_ON(!dev->pdev))
  146. return -EINVAL;
  147. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  148. return -EINVAL;
  149. return drm_pci_irq_by_busid(dev, p);
  150. }
  151. static void drm_pci_agp_init(struct drm_device *dev)
  152. {
  153. if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
  154. if (pci_find_capability(dev->pdev, PCI_CAP_ID_AGP))
  155. dev->agp = drm_agp_init(dev);
  156. if (dev->agp) {
  157. dev->agp->agp_mtrr = arch_phys_wc_add(
  158. dev->agp->agp_info.aper_base,
  159. dev->agp->agp_info.aper_size *
  160. 1024 * 1024);
  161. }
  162. }
  163. }
  164. void drm_pci_agp_destroy(struct drm_device *dev)
  165. {
  166. if (dev->agp) {
  167. arch_phys_wc_del(dev->agp->agp_mtrr);
  168. drm_legacy_agp_clear(dev);
  169. kfree(dev->agp);
  170. dev->agp = NULL;
  171. }
  172. }
  173. /**
  174. * drm_get_pci_dev - Register a PCI device with the DRM subsystem
  175. * @pdev: PCI device
  176. * @ent: entry from the PCI ID table that matches @pdev
  177. * @driver: DRM device driver
  178. *
  179. * Attempt to gets inter module "drm" information. If we are first
  180. * then register the character device and inter module information.
  181. * Try and register, if we fail to register, backout previous work.
  182. *
  183. * NOTE: This function is deprecated, please use drm_dev_alloc() and
  184. * drm_dev_register() instead and remove your &drm_driver.load callback.
  185. *
  186. * Return: 0 on success or a negative error code on failure.
  187. */
  188. int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  189. struct drm_driver *driver)
  190. {
  191. struct drm_device *dev;
  192. int ret;
  193. DRM_DEBUG("\n");
  194. dev = drm_dev_alloc(driver, &pdev->dev);
  195. if (IS_ERR(dev))
  196. return PTR_ERR(dev);
  197. ret = pci_enable_device(pdev);
  198. if (ret)
  199. goto err_free;
  200. dev->pdev = pdev;
  201. #ifdef __alpha__
  202. dev->hose = pdev->sysdata;
  203. #endif
  204. if (drm_core_check_feature(dev, DRIVER_MODESET))
  205. pci_set_drvdata(pdev, dev);
  206. drm_pci_agp_init(dev);
  207. ret = drm_dev_register(dev, ent->driver_data);
  208. if (ret)
  209. goto err_agp;
  210. /* No locking needed since shadow-attach is single-threaded since it may
  211. * only be called from the per-driver module init hook. */
  212. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  213. list_add_tail(&dev->legacy_dev_list, &driver->legacy_dev_list);
  214. return 0;
  215. err_agp:
  216. drm_pci_agp_destroy(dev);
  217. pci_disable_device(pdev);
  218. err_free:
  219. drm_dev_unref(dev);
  220. return ret;
  221. }
  222. EXPORT_SYMBOL(drm_get_pci_dev);
  223. /**
  224. * drm_legacy_pci_init - shadow-attach a legacy DRM PCI driver
  225. * @driver: DRM device driver
  226. * @pdriver: PCI device driver
  227. *
  228. * This is only used by legacy dri1 drivers and deprecated.
  229. *
  230. * Return: 0 on success or a negative error code on failure.
  231. */
  232. int drm_legacy_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
  233. {
  234. struct pci_dev *pdev = NULL;
  235. const struct pci_device_id *pid;
  236. int i;
  237. DRM_DEBUG("\n");
  238. if (WARN_ON(!(driver->driver_features & DRIVER_LEGACY)))
  239. return -EINVAL;
  240. /* If not using KMS, fall back to stealth mode manual scanning. */
  241. INIT_LIST_HEAD(&driver->legacy_dev_list);
  242. for (i = 0; pdriver->id_table[i].vendor != 0; i++) {
  243. pid = &pdriver->id_table[i];
  244. /* Loop around setting up a DRM device for each PCI device
  245. * matching our ID and device class. If we had the internal
  246. * function that pci_get_subsys and pci_get_class used, we'd
  247. * be able to just pass pid in instead of doing a two-stage
  248. * thing.
  249. */
  250. pdev = NULL;
  251. while ((pdev =
  252. pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
  253. pid->subdevice, pdev)) != NULL) {
  254. if ((pdev->class & pid->class_mask) != pid->class)
  255. continue;
  256. /* stealth mode requires a manual probe */
  257. pci_dev_get(pdev);
  258. drm_get_pci_dev(pdev, pid, driver);
  259. }
  260. }
  261. return 0;
  262. }
  263. EXPORT_SYMBOL(drm_legacy_pci_init);
  264. int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask)
  265. {
  266. struct pci_dev *root;
  267. u32 lnkcap, lnkcap2;
  268. *mask = 0;
  269. if (!dev->pdev)
  270. return -EINVAL;
  271. root = dev->pdev->bus->self;
  272. /* we've been informed via and serverworks don't make the cut */
  273. if (root->vendor == PCI_VENDOR_ID_VIA ||
  274. root->vendor == PCI_VENDOR_ID_SERVERWORKS)
  275. return -EINVAL;
  276. pcie_capability_read_dword(root, PCI_EXP_LNKCAP, &lnkcap);
  277. pcie_capability_read_dword(root, PCI_EXP_LNKCAP2, &lnkcap2);
  278. if (lnkcap2) { /* PCIe r3.0-compliant */
  279. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
  280. *mask |= DRM_PCIE_SPEED_25;
  281. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
  282. *mask |= DRM_PCIE_SPEED_50;
  283. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
  284. *mask |= DRM_PCIE_SPEED_80;
  285. } else { /* pre-r3.0 */
  286. if (lnkcap & PCI_EXP_LNKCAP_SLS_2_5GB)
  287. *mask |= DRM_PCIE_SPEED_25;
  288. if (lnkcap & PCI_EXP_LNKCAP_SLS_5_0GB)
  289. *mask |= (DRM_PCIE_SPEED_25 | DRM_PCIE_SPEED_50);
  290. }
  291. DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", root->vendor, root->device, lnkcap, lnkcap2);
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(drm_pcie_get_speed_cap_mask);
  295. int drm_pcie_get_max_link_width(struct drm_device *dev, u32 *mlw)
  296. {
  297. struct pci_dev *root;
  298. u32 lnkcap;
  299. *mlw = 0;
  300. if (!dev->pdev)
  301. return -EINVAL;
  302. root = dev->pdev->bus->self;
  303. pcie_capability_read_dword(root, PCI_EXP_LNKCAP, &lnkcap);
  304. *mlw = (lnkcap & PCI_EXP_LNKCAP_MLW) >> 4;
  305. DRM_INFO("probing mlw for device %x:%x = %x\n", root->vendor, root->device, lnkcap);
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(drm_pcie_get_max_link_width);
  309. #else
  310. void drm_pci_agp_destroy(struct drm_device *dev) {}
  311. int drm_irq_by_busid(struct drm_device *dev, void *data,
  312. struct drm_file *file_priv)
  313. {
  314. return -EINVAL;
  315. }
  316. #endif
  317. /**
  318. * drm_legacy_pci_exit - unregister shadow-attach legacy DRM driver
  319. * @driver: DRM device driver
  320. * @pdriver: PCI device driver
  321. *
  322. * Unregister a DRM driver shadow-attached through drm_legacy_pci_init(). This
  323. * is deprecated and only used by dri1 drivers.
  324. */
  325. void drm_legacy_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver)
  326. {
  327. struct drm_device *dev, *tmp;
  328. DRM_DEBUG("\n");
  329. if (!(driver->driver_features & DRIVER_LEGACY)) {
  330. WARN_ON(1);
  331. } else {
  332. list_for_each_entry_safe(dev, tmp, &driver->legacy_dev_list,
  333. legacy_dev_list) {
  334. list_del(&dev->legacy_dev_list);
  335. drm_put_dev(dev);
  336. }
  337. }
  338. DRM_INFO("Module unloaded\n");
  339. }
  340. EXPORT_SYMBOL(drm_legacy_pci_exit);