Deleted Added
full compact
drm_pci.c (259237) drm_pci.c (280353)
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
24#include <sys/cdefs.h>
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
24#include <sys/cdefs.h>
25__FBSDID("$FreeBSD: stable/10/sys/dev/drm2/drm_pci.c 259237 2013-12-11 23:15:19Z dumbbell $");
25__FBSDID("$FreeBSD: stable/10/sys/dev/drm2/drm_pci.c 280353 2015-03-22 18:32:37Z jah $");
26
27/**
28 * \file drm_pci.h
29 * \brief PCI consistent, DMA-accessible memory allocation.
30 *
31 * \author Eric Anholt <anholt@FreeBSD.org>
32 */
33
34#include <dev/drm2/drmP.h>
35
36/**********************************************************************/
37/** \name PCI memory */
38/*@{*/
39
40static void
41drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
42{
43 drm_dma_handle_t *dmah = arg;
44
45 if (error != 0)
46 return;
47
48 KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
49 dmah->busaddr = segs[0].ds_addr;
50}
51
52/**
53 * \brief Allocate a physically contiguous DMA-accessible consistent
54 * memory block.
55 */
56drm_dma_handle_t *
57drm_pci_alloc(struct drm_device *dev, size_t size,
58 size_t align, dma_addr_t maxaddr)
59{
60 drm_dma_handle_t *dmah;
61 int ret;
62
63 /* Need power-of-two alignment, so fail the allocation if it isn't. */
64 if ((align & (align - 1)) != 0) {
65 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
66 (int)align);
67 return NULL;
68 }
69
70 dmah = malloc(sizeof(drm_dma_handle_t), DRM_MEM_DMA, M_ZERO | M_NOWAIT);
71 if (dmah == NULL)
72 return NULL;
73
74 /* Make sure we aren't holding mutexes here */
75 mtx_assert(&dev->dma_lock, MA_NOTOWNED);
76 if (mtx_owned(&dev->dma_lock))
77 DRM_ERROR("called while holding dma_lock\n");
78
26
27/**
28 * \file drm_pci.h
29 * \brief PCI consistent, DMA-accessible memory allocation.
30 *
31 * \author Eric Anholt <anholt@FreeBSD.org>
32 */
33
34#include <dev/drm2/drmP.h>
35
36/**********************************************************************/
37/** \name PCI memory */
38/*@{*/
39
40static void
41drm_pci_busdma_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
42{
43 drm_dma_handle_t *dmah = arg;
44
45 if (error != 0)
46 return;
47
48 KASSERT(nsegs == 1, ("drm_pci_busdma_callback: bad dma segment count"));
49 dmah->busaddr = segs[0].ds_addr;
50}
51
52/**
53 * \brief Allocate a physically contiguous DMA-accessible consistent
54 * memory block.
55 */
56drm_dma_handle_t *
57drm_pci_alloc(struct drm_device *dev, size_t size,
58 size_t align, dma_addr_t maxaddr)
59{
60 drm_dma_handle_t *dmah;
61 int ret;
62
63 /* Need power-of-two alignment, so fail the allocation if it isn't. */
64 if ((align & (align - 1)) != 0) {
65 DRM_ERROR("drm_pci_alloc with non-power-of-two alignment %d\n",
66 (int)align);
67 return NULL;
68 }
69
70 dmah = malloc(sizeof(drm_dma_handle_t), DRM_MEM_DMA, M_ZERO | M_NOWAIT);
71 if (dmah == NULL)
72 return NULL;
73
74 /* Make sure we aren't holding mutexes here */
75 mtx_assert(&dev->dma_lock, MA_NOTOWNED);
76 if (mtx_owned(&dev->dma_lock))
77 DRM_ERROR("called while holding dma_lock\n");
78
79 ret = bus_dma_tag_create(NULL, align, 0, /* tag, align, boundary */
79 ret = bus_dma_tag_create(
80 bus_get_dma_tag(dev->device), /* parent */
81 align, 0, /* align, boundary */
80 maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
81 NULL, NULL, /* filtfunc, filtfuncargs */
82 size, 1, size, /* maxsize, nsegs, maxsegsize */
83 0, NULL, NULL, /* flags, lockfunc, lockfuncargs */
84 &dmah->tag);
85 if (ret != 0) {
86 free(dmah, DRM_MEM_DMA);
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 free(dmah, DRM_MEM_DMA);
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 free(dmah, DRM_MEM_DMA);
104 return NULL;
105 }
106
107 return dmah;
108}
109
110/**
111 * \brief Free a DMA-accessible consistent memory block.
112 */
113void
114drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah)
115{
116 if (dmah == NULL)
117 return;
118
119 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
120 bus_dma_tag_destroy(dmah->tag);
121
122 free(dmah, DRM_MEM_DMA);
123}
124
125/*@}*/
126
127int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *mask)
128{
129 device_t root;
130 int pos;
131 u32 lnkcap = 0, lnkcap2 = 0;
132
133 *mask = 0;
134 if (!drm_device_is_pcie(dev))
135 return -EINVAL;
136
137 root =
138 device_get_parent( /* pcib */
139 device_get_parent( /* `-- pci */
140 device_get_parent( /* `-- vgapci */
141 dev->device))); /* `-- drmn */
142
143 pos = 0;
144 pci_find_cap(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_LINK_CAP, 4);
154 lnkcap2 = pci_read_config(root, pos + PCIER_LINK_CAP2, 4);
155
156 lnkcap &= PCIEM_LINK_CAP_MAX_SPEED;
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}
82 maxaddr, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
83 NULL, NULL, /* filtfunc, filtfuncargs */
84 size, 1, size, /* maxsize, nsegs, maxsegsize */
85 0, NULL, NULL, /* flags, lockfunc, lockfuncargs */
86 &dmah->tag);
87 if (ret != 0) {
88 free(dmah, DRM_MEM_DMA);
89 return NULL;
90 }
91
92 ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr,
93 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_NOCACHE, &dmah->map);
94 if (ret != 0) {
95 bus_dma_tag_destroy(dmah->tag);
96 free(dmah, DRM_MEM_DMA);
97 return NULL;
98 }
99
100 ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr, size,
101 drm_pci_busdma_callback, dmah, BUS_DMA_NOWAIT);
102 if (ret != 0) {
103 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
104 bus_dma_tag_destroy(dmah->tag);
105 free(dmah, DRM_MEM_DMA);
106 return NULL;
107 }
108
109 return dmah;
110}
111
112/**
113 * \brief Free a DMA-accessible consistent memory block.
114 */
115void
116drm_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 free(dmah, DRM_MEM_DMA);
125}
126
127/*@}*/
128
129int 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 =
140 device_get_parent( /* pcib */
141 device_get_parent( /* `-- pci */
142 device_get_parent( /* `-- vgapci */
143 dev->device))); /* `-- drmn */
144
145 pos = 0;
146 pci_find_cap(root, PCIY_EXPRESS, &pos);
147 if (!pos)
148 return -EINVAL;
149
150 /* we've been informed via and serverworks don't make the cut */
151 if (pci_get_vendor(root) == PCI_VENDOR_ID_VIA ||
152 pci_get_vendor(root) == PCI_VENDOR_ID_SERVERWORKS)
153 return -EINVAL;
154
155 lnkcap = pci_read_config(root, pos + PCIER_LINK_CAP, 4);
156 lnkcap2 = pci_read_config(root, pos + PCIER_LINK_CAP2, 4);
157
158 lnkcap &= PCIEM_LINK_CAP_MAX_SPEED;
159 lnkcap2 &= 0xfe;
160
161#define PCI_EXP_LNKCAP2_SLS_2_5GB 0x02 /* Supported Link Speed 2.5GT/s */
162#define PCI_EXP_LNKCAP2_SLS_5_0GB 0x04 /* Supported Link Speed 5.0GT/s */
163#define PCI_EXP_LNKCAP2_SLS_8_0GB 0x08 /* Supported Link Speed 8.0GT/s */
164
165 if (lnkcap2) { /* PCIE GEN 3.0 */
166 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
167 *mask |= DRM_PCIE_SPEED_25;
168 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
169 *mask |= DRM_PCIE_SPEED_50;
170 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
171 *mask |= DRM_PCIE_SPEED_80;
172 } else {
173 if (lnkcap & 1)
174 *mask |= DRM_PCIE_SPEED_25;
175 if (lnkcap & 2)
176 *mask |= DRM_PCIE_SPEED_50;
177 }
178
179 DRM_INFO("probing gen 2 caps for device %x:%x = %x/%x\n", pci_get_vendor(root), pci_get_device(root), lnkcap, lnkcap2);
180 return 0;
181}