drm_pci.c 12 KB

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