drm_pci.c 12 KB

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