1 /* 2 * Copyright 2003 José Fonseca. 3 * Copyright 2003 Leif Delgass. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 #include <drm/drmP.h> 26 #include "drm_legacy.h" 27 #include "drm_internal.h" 28 29 /**********************************************************************/ 30 /** \name PCI memory */ 31 /*@{*/ 32 33 static void 34 drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 35 { 36 drm_dma_handle_t *dmah = arg; 37 38 if (error != 0) 39 return; 40 41 KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count")); 42 dmah->busaddr = segs[0].ds_addr; 43 } 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 int ret; 52 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 60 /* Need power-of-two alignment, so fail the allocation if it isn't. */ 61 if ((align & (align - 1)) != 0) { 62 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n", 63 (int)align); 64 return NULL; 65 } 66 67 dmah = kmalloc(sizeof(drm_dma_handle_t), M_DRM, M_WAITOK | M_NULLOK); 68 if (!dmah) 69 return NULL; 70 71 dmah->size = size; 72 73 ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */ 74 ~0, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */ 75 NULL, NULL, /* filtfunc, filtfuncargs */ 76 size, 1, size, /* maxsize, nsegs, maxsegsize */ 77 0, /* flags */ 78 &dmah->tag); 79 if (ret != 0) { 80 drm_free(dmah, M_DRM); 81 return NULL; 82 } 83 84 ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr, 85 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_NOCACHE, &dmah->map); 86 if (ret != 0) { 87 bus_dma_tag_destroy(dmah->tag); 88 drm_free(dmah, M_DRM); 89 return NULL; 90 } 91 92 ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size, 93 drm_pci_busdma_callback, dmah, BUS_DMA_NOWAIT); 94 if (ret != 0) { 95 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map); 96 bus_dma_tag_destroy(dmah->tag); 97 drm_free(dmah, M_DRM); 98 return NULL; 99 } 100 101 memset(dmah->vaddr, 0, size); 102 103 return dmah; 104 } 105 106 /* 107 * Free a PCI consistent memory block without freeing its descriptor. 108 * 109 * This function is for internal use in the Linux-specific DRM core code. 110 */ 111 void __drm_legacy_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah) 112 { 113 if (dmah == NULL) 114 return; 115 116 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map); 117 bus_dma_tag_destroy(dmah->tag); 118 } 119 120 /** 121 * drm_pci_free - Free a PCI consistent memory block 122 * @dev: DRM device 123 * @dmah: handle to memory block 124 */ 125 void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah) 126 { 127 __drm_legacy_pci_free(dev, dmah); 128 kfree(dmah); 129 } 130 131 int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask) 132 { 133 device_t root; 134 int pos; 135 u32 lnkcap = 0, lnkcap2 = 0; 136 137 *mask = 0; 138 if (!drm_device_is_pcie(dev)) 139 return -EINVAL; 140 141 root = device_get_parent(dev->dev); 142 143 pos = 0; 144 pci_find_extcap(root, PCIY_EXPRESS, &pos); 145 if (!pos) 146 return -EINVAL; 147 148 /* we've been informed via and serverworks don't make the cut */ 149 if (pci_get_vendor(root) == PCI_VENDOR_ID_VIA || 150 pci_get_vendor(root) == PCI_VENDOR_ID_SERVERWORKS) 151 return -EINVAL; 152 153 lnkcap = pci_read_config(root, pos + PCIER_LINKCAP, 4); 154 lnkcap2 = pci_read_config(root, pos + PCIER_LINK_CAP2, 4); 155 156 lnkcap &= PCIEM_LNKCAP_SPEED_MASK; 157 lnkcap2 &= 0xfe; 158 159 #define PCI_EXP_LNKCAP2_SLS_2_5GB 0x02 /* Supported Link Speed 2.5GT/s */ 160 #define PCI_EXP_LNKCAP2_SLS_5_0GB 0x04 /* Supported Link Speed 5.0GT/s */ 161 #define PCI_EXP_LNKCAP2_SLS_8_0GB 0x08 /* Supported Link Speed 8.0GT/s */ 162 163 if (lnkcap2) { /* PCIE GEN 3.0 */ 164 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB) 165 *mask |= DRM_PCIE_SPEED_25; 166 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB) 167 *mask |= DRM_PCIE_SPEED_50; 168 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB) 169 *mask |= DRM_PCIE_SPEED_80; 170 } else { 171 if (lnkcap & 1) 172 *mask |= DRM_PCIE_SPEED_25; 173 if (lnkcap & 2) 174 *mask |= DRM_PCIE_SPEED_50; 175 } 176 177 DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", pci_get_vendor(root), pci_get_device(root), lnkcap, lnkcap2); 178 return 0; 179 } 180 181 /** 182 * Get interrupt from bus id. 183 * 184 * \param inode device inode. 185 * \param file_priv DRM file private. 186 * \param cmd command. 187 * \param arg user argument, pointing to a drm_irq_busid structure. 188 * \return zero on success or a negative number on failure. 189 * 190 * Finds the PCI device with the specified bus id and gets its IRQ number. 191 * This IOCTL is deprecated, and will now return EINVAL for any busid not equal 192 * to that of the device that this DRM instance attached to. 193 */ 194 int drm_irq_by_busid(struct drm_device *dev, void *data, 195 struct drm_file *file_priv) 196 { 197 struct drm_irq_busid *irq = data; 198 199 if ((irq->busnum >> 8) != dev->pci_domain || 200 (irq->busnum & 0xff) != dev->pci_bus || 201 irq->devnum != dev->pci_slot || 202 irq->funcnum != dev->pci_func) 203 return -EINVAL; 204 205 irq->irq = dev->irq; 206 207 DRM_DEBUG("%d:%d:%d => IRQ %d\n", 208 irq->busnum, irq->devnum, irq->funcnum, irq->irq); 209 210 return 0; 211 } 212