drm_pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 (drm_core_has_MTRR(dev)) {
  233. if (dev->agp)
  234. dev->agp->agp_mtrr = arch_phys_wc_add(
  235. dev->agp->agp_info.aper_base,
  236. dev->agp->agp_info.aper_size *
  237. 1024 * 1024);
  238. }
  239. }
  240. return 0;
  241. }
  242. static struct drm_bus drm_pci_bus = {
  243. .bus_type = DRIVER_BUS_PCI,
  244. .get_irq = drm_pci_get_irq,
  245. .get_name = drm_pci_get_name,
  246. .set_busid = drm_pci_set_busid,
  247. .set_unique = drm_pci_set_unique,
  248. .irq_by_busid = drm_pci_irq_by_busid,
  249. .agp_init = drm_pci_agp_init,
  250. };
  251. /**
  252. * Register.
  253. *
  254. * \param pdev - PCI device structure
  255. * \param ent entry from the PCI ID table with device type flags
  256. * \return zero on success or a negative number on failure.
  257. *
  258. * Attempt to gets inter module "drm" information. If we are first
  259. * then register the character device and inter module information.
  260. * Try and register, if we fail to register, backout previous work.
  261. */
  262. int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  263. struct drm_driver *driver)
  264. {
  265. struct drm_device *dev;
  266. int ret;
  267. DRM_DEBUG("\n");
  268. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  269. if (!dev)
  270. return -ENOMEM;
  271. ret = pci_enable_device(pdev);
  272. if (ret)
  273. goto err_g1;
  274. dev->pdev = pdev;
  275. dev->dev = &pdev->dev;
  276. dev->pci_device = pdev->device;
  277. dev->pci_vendor = pdev->vendor;
  278. #ifdef __alpha__
  279. dev->hose = pdev->sysdata;
  280. #endif
  281. mutex_lock(&drm_global_mutex);
  282. if ((ret = drm_fill_in_dev(dev, ent, driver))) {
  283. printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  284. goto err_g2;
  285. }
  286. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  287. pci_set_drvdata(pdev, dev);
  288. ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
  289. if (ret)
  290. goto err_g2;
  291. }
  292. if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
  293. goto err_g3;
  294. if (dev->driver->load) {
  295. ret = dev->driver->load(dev, ent->driver_data);
  296. if (ret)
  297. goto err_g4;
  298. }
  299. /* setup the grouping for the legacy output */
  300. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  301. ret = drm_mode_group_init_legacy_group(dev,
  302. &dev->primary->mode_group);
  303. if (ret)
  304. goto err_g4;
  305. }
  306. list_add_tail(&dev->driver_item, &driver->device_list);
  307. DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
  308. driver->name, driver->major, driver->minor, driver->patchlevel,
  309. driver->date, pci_name(pdev), dev->primary->index);
  310. mutex_unlock(&drm_global_mutex);
  311. return 0;
  312. err_g4:
  313. drm_put_minor(&dev->primary);
  314. err_g3:
  315. if (drm_core_check_feature(dev, DRIVER_MODESET))
  316. drm_put_minor(&dev->control);
  317. err_g2:
  318. pci_disable_device(pdev);
  319. err_g1:
  320. kfree(dev);
  321. mutex_unlock(&drm_global_mutex);
  322. return ret;
  323. }
  324. EXPORT_SYMBOL(drm_get_pci_dev);
  325. /**
  326. * PCI device initialization. Called direct from modules at load time.
  327. *
  328. * \return zero on success or a negative number on failure.
  329. *
  330. * Initializes a drm_device structures,registering the
  331. * stubs and initializing the AGP device.
  332. *
  333. * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and
  334. * after the initialization for driver customization.
  335. */
  336. int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
  337. {
  338. struct pci_dev *pdev = NULL;
  339. const struct pci_device_id *pid;
  340. int i;
  341. DRM_DEBUG("\n");
  342. INIT_LIST_HEAD(&driver->device_list);
  343. driver->kdriver.pci = pdriver;
  344. driver->bus = &drm_pci_bus;
  345. if (driver->driver_features & DRIVER_MODESET)
  346. return pci_register_driver(pdriver);
  347. /* If not using KMS, fall back to stealth mode manual scanning. */
  348. for (i = 0; pdriver->id_table[i].vendor != 0; i++) {
  349. pid = &pdriver->id_table[i];
  350. /* Loop around setting up a DRM device for each PCI device
  351. * matching our ID and device class. If we had the internal
  352. * function that pci_get_subsys and pci_get_class used, we'd
  353. * be able to just pass pid in instead of doing a two-stage
  354. * thing.
  355. */
  356. pdev = NULL;
  357. while ((pdev =
  358. pci_get_subsys(pid->vendor, pid->device, pid->subvendor,
  359. pid->subdevice, pdev)) != NULL) {
  360. if ((pdev->class & pid->class_mask) != pid->class)
  361. continue;
  362. /* stealth mode requires a manual probe */
  363. pci_dev_get(pdev);
  364. drm_get_pci_dev(pdev, pid, driver);
  365. }
  366. }
  367. return 0;
  368. }
  369. int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask)
  370. {
  371. struct pci_dev *root;
  372. u32 lnkcap, lnkcap2;
  373. *mask = 0;
  374. if (!dev->pdev)
  375. return -EINVAL;
  376. root = dev->pdev->bus->self;
  377. /* we've been informed via and serverworks don't make the cut */
  378. if (root->vendor == PCI_VENDOR_ID_VIA ||
  379. root->vendor == PCI_VENDOR_ID_SERVERWORKS)
  380. return -EINVAL;
  381. pcie_capability_read_dword(root, PCI_EXP_LNKCAP, &lnkcap);
  382. pcie_capability_read_dword(root, PCI_EXP_LNKCAP2, &lnkcap2);
  383. if (lnkcap2) { /* PCIe r3.0-compliant */
  384. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
  385. *mask |= DRM_PCIE_SPEED_25;
  386. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
  387. *mask |= DRM_PCIE_SPEED_50;
  388. if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
  389. *mask |= DRM_PCIE_SPEED_80;
  390. } else { /* pre-r3.0 */
  391. if (lnkcap & PCI_EXP_LNKCAP_SLS_2_5GB)
  392. *mask |= DRM_PCIE_SPEED_25;
  393. if (lnkcap & PCI_EXP_LNKCAP_SLS_5_0GB)
  394. *mask |= (DRM_PCIE_SPEED_25 | DRM_PCIE_SPEED_50);
  395. }
  396. DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", root->vendor, root->device, lnkcap, lnkcap2);
  397. return 0;
  398. }
  399. EXPORT_SYMBOL(drm_pcie_get_speed_cap_mask);
  400. #else
  401. int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver)
  402. {
  403. return -1;
  404. }
  405. #endif
  406. EXPORT_SYMBOL(drm_pci_init);
  407. /*@}*/
  408. void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver)
  409. {
  410. struct drm_device *dev, *tmp;
  411. DRM_DEBUG("\n");
  412. if (driver->driver_features & DRIVER_MODESET) {
  413. pci_unregister_driver(pdriver);
  414. } else {
  415. list_for_each_entry_safe(dev, tmp, &driver->device_list, driver_item)
  416. drm_put_dev(dev);
  417. }
  418. DRM_INFO("Module unloaded\n");
  419. }
  420. EXPORT_SYMBOL(drm_pci_exit);