xref: /dragonfly/sys/dev/drm/drm_pci.c (revision 820c5b08)
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/drm_legacy.h>
27 
28 /**********************************************************************/
29 /** \name PCI memory */
30 /*@{*/
31 
32 static void
33 drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
34 {
35 	drm_dma_handle_t *dmah = arg;
36 
37 	if (error != 0)
38 		return;
39 
40 	KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
41 	dmah->busaddr = segs[0].ds_addr;
42 }
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 	int ret;
51 
52 	/* pci_alloc_consistent only guarantees alignment to the smallest
53 	 * PAGE_SIZE order which is greater than or equal to the requested size.
54 	 * Return NULL here for now to make sure nobody tries for larger alignment
55 	 */
56 	if (align > size)
57 		return NULL;
58 
59 	/* Need power-of-two alignment, so fail the allocation if it isn't. */
60 	if ((align & (align - 1)) != 0) {
61 		DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
62 		    (int)align);
63 		return NULL;
64 	}
65 
66 	dmah = kmalloc(sizeof(drm_dma_handle_t), M_DRM, M_WAITOK | M_NULLOK);
67 	if (!dmah)
68 		return NULL;
69 
70 	dmah->size = size;
71 
72 	ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
73 	    ~0, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
74 	    NULL, NULL, /* filtfunc, filtfuncargs */
75 	    size, 1, size, /* maxsize, nsegs, maxsegsize */
76 	    0,		/* flags */
77 	    &dmah->tag);
78 	if (ret != 0) {
79 		drm_free(dmah, M_DRM);
80 		return NULL;
81 	}
82 
83 	ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr,
84 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_NOCACHE, &dmah->map);
85 	if (ret != 0) {
86 		bus_dma_tag_destroy(dmah->tag);
87 		drm_free(dmah, M_DRM);
88 		return NULL;
89 	}
90 
91 	ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
92 	    drm_pci_busdma_callback, dmah, BUS_DMA_NOWAIT);
93 	if (ret != 0) {
94 		bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
95 		bus_dma_tag_destroy(dmah->tag);
96 		drm_free(dmah, M_DRM);
97 		return NULL;
98 	}
99 
100 	memset(dmah->vaddr, 0, size);
101 
102 	return dmah;
103 }
104 
105 /*
106  * Free a PCI consistent memory block without freeing its descriptor.
107  *
108  * This function is for internal use in the Linux-specific DRM core code.
109  */
110 void __drm_legacy_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
111 {
112 	if (dmah == NULL)
113 		return;
114 
115 	bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
116 	bus_dma_tag_destroy(dmah->tag);
117 }
118 
119 /**
120  * drm_pci_free - Free a PCI consistent memory block
121  * @dev: DRM device
122  * @dmah: handle to memory block
123  */
124 void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
125 {
126 	__drm_legacy_pci_free(dev, dmah);
127 	kfree(dmah);
128 }
129 
130 int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask)
131 {
132 	device_t root;
133 	int pos;
134 	u32 lnkcap = 0, lnkcap2 = 0;
135 
136 	*mask = 0;
137 	if (!drm_device_is_pcie(dev))
138 		return -EINVAL;
139 
140 	root = device_get_parent(dev->dev);
141 
142 	pos = 0;
143 	pci_find_extcap(root, PCIY_EXPRESS, &pos);
144 	if (!pos)
145 		return -EINVAL;
146 
147 	/* we've been informed via and serverworks don't make the cut */
148 	if (pci_get_vendor(root) == PCI_VENDOR_ID_VIA ||
149 	    pci_get_vendor(root) == PCI_VENDOR_ID_SERVERWORKS)
150 		return -EINVAL;
151 
152 	lnkcap = pci_read_config(root, pos + PCIER_LINKCAP, 4);
153 	lnkcap2 = pci_read_config(root, pos + PCIER_LINK_CAP2, 4);
154 
155 	lnkcap &= PCIEM_LNKCAP_SPEED_MASK;
156 	lnkcap2 &= 0xfe;
157 
158 #define	PCI_EXP_LNKCAP2_SLS_2_5GB 0x02	/* Supported Link Speed 2.5GT/s */
159 #define	PCI_EXP_LNKCAP2_SLS_5_0GB 0x04	/* Supported Link Speed 5.0GT/s */
160 #define	PCI_EXP_LNKCAP2_SLS_8_0GB 0x08	/* Supported Link Speed 8.0GT/s */
161 
162 	if (lnkcap2) { /* PCIE GEN 3.0 */
163 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
164 			*mask |= DRM_PCIE_SPEED_25;
165 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
166 			*mask |= DRM_PCIE_SPEED_50;
167 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
168 			*mask |= DRM_PCIE_SPEED_80;
169 	} else {
170 		if (lnkcap & 1)
171 			*mask |= DRM_PCIE_SPEED_25;
172 		if (lnkcap & 2)
173 			*mask |= DRM_PCIE_SPEED_50;
174 	}
175 
176 	DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", pci_get_vendor(root), pci_get_device(root), lnkcap, lnkcap2);
177 	return 0;
178 }
179 
180 /**
181  * Get interrupt from bus id.
182  *
183  * \param inode device inode.
184  * \param file_priv DRM file private.
185  * \param cmd command.
186  * \param arg user argument, pointing to a drm_irq_busid structure.
187  * \return zero on success or a negative number on failure.
188  *
189  * Finds the PCI device with the specified bus id and gets its IRQ number.
190  * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
191  * to that of the device that this DRM instance attached to.
192  */
193 int drm_irq_by_busid(struct drm_device *dev, void *data,
194 		     struct drm_file *file_priv)
195 {
196 	struct drm_irq_busid *irq = data;
197 
198 	if ((irq->busnum >> 8) != dev->pci_domain ||
199 	    (irq->busnum & 0xff) != dev->pci_bus ||
200 	    irq->devnum != dev->pci_slot ||
201 	    irq->funcnum != dev->pci_func)
202 		return -EINVAL;
203 
204 	irq->irq = dev->irq;
205 
206 	DRM_DEBUG("%d:%d:%d => IRQ %d\n",
207 	    irq->busnum, irq->devnum, irq->funcnum, irq->irq);
208 
209 	return 0;
210 }
211