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