drm_pci.c 12 KB

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