1 /* drm_pci.h -- PCI DMA memory management wrappers for DRM -*- linux-c -*- */
4 * \brief Functions and ioctls to manage PCI memory
6 * \warning These interfaces aren't stable yet.
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..
11 * \author José Fonseca <jrfonseca@tungstengraphics.com>
12 * \author Leif Delgass <ldelgass@retinalburn.net>
16 * Copyright 2003 José Fonseca.
17 * Copyright 2003 Leif Delgass.
18 * All Rights Reserved.
20 * Permission is hereby granted, free of charge, to any person obtaining a
21 * copy of this software and associated documentation files (the "Software"),
22 * to deal in the Software without restriction, including without limitation
23 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
24 * and/or sell copies of the Software, and to permit persons to whom the
25 * Software is furnished to do so, subject to the following conditions:
27 * The above copyright notice and this permission notice (including the next
28 * paragraph) shall be included in all copies or substantial portions of the
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
35 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <dev/drm2/drmP.h>
44 static int drm_msi = 1; /* Enable by default. */
45 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
46 SYSCTL_INT(_hw_drm, OID_AUTO, msi, CTLFLAG_RDTUN, &drm_msi, 1,
47 "Enable MSI interrupts for drm devices");
49 /**********************************************************************/
50 /** \name PCI memory */
54 drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
56 drm_dma_handle_t *dmah = arg;
61 KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
62 dmah->busaddr = segs[0].ds_addr;
66 * \brief Allocate a PCI consistent memory block, for DMA.
68 drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size,
69 size_t align, dma_addr_t maxaddr)
71 drm_dma_handle_t *dmah;
74 /* Need power-of-two alignment, so fail the allocation if it isn't. */
75 if ((align & (align - 1)) != 0) {
76 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
81 dmah = malloc(sizeof(drm_dma_handle_t), DRM_MEM_DMA, M_ZERO | M_NOWAIT);
85 /* Make sure we aren't holding mutexes here */
86 mtx_assert(&dev->dma_lock, MA_NOTOWNED);
87 if (mtx_owned(&dev->dma_lock))
88 DRM_ERROR("called while holding dma_lock\n");
90 ret = bus_dma_tag_create(
91 bus_get_dma_tag(dev->dev), /* parent */
92 align, 0, /* align, boundary */
93 maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
94 NULL, NULL, /* filtfunc, filtfuncargs */
95 size, 1, size, /* maxsize, nsegs, maxsegsize */
96 0, NULL, NULL, /* flags, lockfunc, lockfuncargs */
99 free(dmah, DRM_MEM_DMA);
103 ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr,
104 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_NOCACHE, &dmah->map);
106 bus_dma_tag_destroy(dmah->tag);
107 free(dmah, DRM_MEM_DMA);
111 ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
112 drm_pci_busdma_callback, dmah, BUS_DMA_NOWAIT);
114 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
115 bus_dma_tag_destroy(dmah->tag);
116 free(dmah, DRM_MEM_DMA);
123 EXPORT_SYMBOL(drm_pci_alloc);
126 * \brief Free a PCI consistent memory block without freeing its descriptor.
128 * This function is for internal use in the Linux-specific DRM core code.
130 void __drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
135 bus_dmamap_unload(dmah->tag, dmah->map);
136 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
137 bus_dma_tag_destroy(dmah->tag);
141 * \brief Free a PCI consistent memory block
143 void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
145 __drm_pci_free(dev, dmah);
146 free(dmah, DRM_MEM_DMA);
149 EXPORT_SYMBOL(drm_pci_free);
151 static int drm_get_pci_domain(struct drm_device *dev)
153 return dev->pci_domain;
156 static int drm_pci_get_irq(struct drm_device *dev)
162 dev->irqr = bus_alloc_resource_any(dev->dev, SYS_RES_IRQ,
163 &dev->irqrid, RF_SHAREABLE);
165 dev_err(dev->dev, "Failed to allocate IRQ\n");
169 dev->irq = (int) rman_get_start(dev->irqr);
174 static void drm_pci_free_irq(struct drm_device *dev)
176 if (dev->irqr == NULL)
179 bus_release_resource(dev->dev, SYS_RES_IRQ,
180 dev->irqrid, dev->irqr);
186 static const char *drm_pci_get_name(struct drm_device *dev)
188 return dev->driver->name;
191 int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
194 master->unique_len = 40;
195 master->unique_size = master->unique_len;
196 master->unique = malloc(master->unique_size, DRM_MEM_DRIVER, M_NOWAIT);
197 if (master->unique == NULL)
201 len = snprintf(master->unique, master->unique_len,
202 "pci:%04x:%02x:%02x.%d",
208 if (len >= master->unique_len) {
209 DRM_ERROR("buffer overflow");
213 master->unique_len = len;
220 int drm_pci_set_unique(struct drm_device *dev,
221 struct drm_master *master,
222 struct drm_unique *u)
224 int domain, bus, slot, func, ret;
226 master->unique_len = u->unique_len;
227 master->unique_size = u->unique_len + 1;
228 master->unique = malloc(master->unique_size, DRM_MEM_DRIVER, M_WAITOK);
229 if (!master->unique) {
234 if (copy_from_user(master->unique, u->unique, master->unique_len)) {
239 master->unique[master->unique_len] = '\0';
241 /* Return error if the busid submitted doesn't match the device's actual
244 ret = sscanf(master->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
253 if ((domain != dev->pci_domain) ||
254 (bus != dev->pci_bus) ||
255 (slot != dev->pci_slot) ||
256 (func != dev->pci_func)) {
266 static int drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *p)
268 if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
269 (p->busnum & 0xff) != dev->pci_bus ||
270 p->devnum != dev->pci_slot || p->funcnum != dev->pci_func)
275 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
280 int drm_pci_agp_init(struct drm_device *dev)
282 if (drm_core_has_AGP(dev)) {
283 if (drm_pci_device_is_agp(dev))
284 dev->agp = drm_agp_init(dev);
285 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
286 && (dev->agp == NULL)) {
287 DRM_ERROR("Cannot initialize the agpgart module.\n");
290 if (drm_core_has_MTRR(dev)) {
291 if (dev->agp && dev->agp->agp_info.ai_aperture_base != 0) {
292 if (drm_mtrr_add(dev->agp->agp_info.ai_aperture_base,
293 dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC) == 0)
294 dev->agp->agp_mtrr = 1;
296 dev->agp->agp_mtrr = -1;
303 static struct drm_bus drm_pci_bus = {
304 .bus_type = DRIVER_BUS_PCI,
305 .get_irq = drm_pci_get_irq,
306 .free_irq = drm_pci_free_irq,
307 .get_name = drm_pci_get_name,
308 .set_busid = drm_pci_set_busid,
309 .set_unique = drm_pci_set_unique,
310 .irq_by_busid = drm_pci_irq_by_busid,
311 .agp_init = drm_pci_agp_init,
317 * \param pdev - PCI device structure
318 * \param ent entry from the PCI ID table with device type flags
319 * \return zero on success or a negative number on failure.
321 * Attempt to gets inter module "drm" information. If we are first
322 * then register the character device and inter module information.
323 * Try and register, if we fail to register, backout previous work.
325 int drm_get_pci_dev(device_t kdev, struct drm_device *dev,
326 struct drm_driver *driver)
332 driver->bus = &drm_pci_bus;
336 dev->pci_domain = pci_get_domain(dev->dev);
337 dev->pci_bus = pci_get_bus(dev->dev);
338 dev->pci_slot = pci_get_slot(dev->dev);
339 dev->pci_func = pci_get_function(dev->dev);
341 dev->pci_vendor = pci_get_vendor(dev->dev);
342 dev->pci_device = pci_get_device(dev->dev);
343 dev->pci_subvendor = pci_get_subvendor(dev->dev);
344 dev->pci_subdevice = pci_get_subdevice(dev->dev);
346 sx_xlock(&drm_global_mutex);
348 if ((ret = drm_fill_in_dev(dev, driver))) {
349 DRM_ERROR("Failed to fill in dev: %d\n", ret);
353 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
354 ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
359 if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
362 if (dev->driver->load) {
363 ret = dev->driver->load(dev,
364 dev->id_entry->driver_private);
369 /* setup the grouping for the legacy output */
370 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
371 ret = drm_mode_group_init_legacy_group(dev,
372 &dev->primary->mode_group);
377 #ifdef FREEBSD_NOTYET
378 list_add_tail(&dev->driver_item, &driver->device_list);
379 #endif /* FREEBSD_NOTYET */
381 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
382 driver->name, driver->major, driver->minor, driver->patchlevel,
383 driver->date, device_get_nameunit(dev->dev), dev->primary->index);
385 sx_xunlock(&drm_global_mutex);
389 if (dev->driver->unload)
390 dev->driver->unload(dev);
392 drm_put_minor(&dev->primary);
394 if (drm_core_check_feature(dev, DRIVER_MODESET))
395 drm_put_minor(&dev->control);
397 drm_cancel_fill_in_dev(dev);
399 sx_xunlock(&drm_global_mutex);
402 EXPORT_SYMBOL(drm_get_pci_dev);
405 drm_pci_enable_msi(struct drm_device *dev)
412 msicount = pci_msi_count(dev->dev);
413 DRM_DEBUG("MSI count = %d\n", msicount);
417 ret = pci_alloc_msi(dev->dev, &msicount);
419 DRM_INFO("MSI enabled %d message(s)\n", msicount);
420 dev->msi_enabled = 1;
428 drm_pci_disable_msi(struct drm_device *dev)
431 if (!dev->msi_enabled)
434 pci_release_msi(dev->dev);
435 dev->msi_enabled = 0;
439 int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask)
443 u32 lnkcap = 0, lnkcap2 = 0;
446 if (!drm_pci_device_is_pcie(dev))
450 device_get_parent( /* pcib */
451 device_get_parent( /* `-- pci */
452 device_get_parent( /* `-- vgapci */
453 dev->dev))); /* `-- drmn */
456 pci_find_cap(root, PCIY_EXPRESS, &pos);
460 /* we've been informed via and serverworks don't make the cut */
461 if (pci_get_vendor(root) == PCI_VENDOR_ID_VIA ||
462 pci_get_vendor(root) == PCI_VENDOR_ID_SERVERWORKS)
465 lnkcap = pci_read_config(root, pos + PCIER_LINK_CAP, 4);
466 lnkcap2 = pci_read_config(root, pos + PCIER_LINK_CAP2, 4);
468 lnkcap &= PCIEM_LINK_CAP_MAX_SPEED;
471 #define PCI_EXP_LNKCAP2_SLS_2_5GB 0x02 /* Supported Link Speed 2.5GT/s */
472 #define PCI_EXP_LNKCAP2_SLS_5_0GB 0x04 /* Supported Link Speed 5.0GT/s */
473 #define PCI_EXP_LNKCAP2_SLS_8_0GB 0x08 /* Supported Link Speed 8.0GT/s */
475 if (lnkcap2) { /* PCIE GEN 3.0 */
476 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
477 *mask |= DRM_PCIE_SPEED_25;
478 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
479 *mask |= DRM_PCIE_SPEED_50;
480 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
481 *mask |= DRM_PCIE_SPEED_80;
484 *mask |= DRM_PCIE_SPEED_25;
486 *mask |= DRM_PCIE_SPEED_50;
489 DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", pci_get_vendor(root), pci_get_device(root), lnkcap, lnkcap2);
492 EXPORT_SYMBOL(drm_pcie_get_speed_cap_mask);