drm_pci.c 12 KB

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