drm_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. static 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. .irq_by_busid = drm_pci_irq_by_busid,
  252. .agp_init = drm_pci_agp_init,
  253. };
  254. /**
  255. * Register.
  256. *
  257. * \param pdev - PCI device structure
  258. * \param ent entry from the PCI ID table with device type flags
  259. * \return zero on success or a negative number on failure.
  260. *
  261. * Attempt to gets inter module "drm" information. If we are first
  262. * then register the character device and inter module information.
  263. * Try and register, if we fail to register, backout previous work.
  264. */
  265. int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  266. struct drm_driver *driver)
  267. {
  268. struct drm_device *dev;
  269. int ret;
  270. DRM_DEBUG("\n");
  271. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  272. if (!dev)
  273. return -ENOMEM;
  274. ret = pci_enable_device(pdev);
  275. if (ret)
  276. goto err_g1;
  277. pci_set_master(pdev);
  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);