drm_pci.c revision 1.37 1 /* $NetBSD: drm_pci.c,v 1.37 2021/12/19 09:51:04 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_pci.c,v 1.37 2021/12/19 09:51:04 riastradh Exp $");
34
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/systm.h>
38
39 #include <dev/pci/pcivar.h>
40
41 #include <drm/drmP.h>
42 #include <drm/drm_drv.h>
43 #include <drm/drm_legacy.h>
44
45 #include "../dist/drm/drm_internal.h"
46
47 struct drm_bus_irq_cookie {
48 pci_intr_handle_t *intr_handles;
49 void *ih_cookie;
50 };
51
52 static const struct pci_attach_args *
53 drm_pci_attach_args(struct drm_device *dev)
54 {
55 return &dev->pdev->pd_pa;
56 }
57
58 int
59 drm_pci_attach(device_t self, const struct pci_attach_args *pa,
60 struct pci_dev *pdev, struct drm_driver *driver, unsigned long cookie,
61 struct drm_device **devp)
62 {
63 struct drm_device *dev;
64 unsigned int unit;
65 int ret;
66
67 /* Ensure the drm agp hooks are initialized. */
68 /* XXX errno NetBSD->Linux */
69 ret = -drm_guarantee_initialized();
70 if (ret)
71 goto fail0;
72
73 /* Create a DRM device. */
74 dev = drm_dev_alloc(driver, self);
75 if (dev == NULL) {
76 ret = -ENOMEM;
77 goto fail0;
78 }
79
80 dev->pdev = pdev;
81
82 /* XXX Set the power state to D0? */
83
84 /* Set up the bus space and bus DMA tags. */
85 dev->bst = pa->pa_memt;
86 dev->bus_dmat = (pci_dma64_available(pa)? pa->pa_dmat64 : pa->pa_dmat);
87 dev->bus_dmat32 = pa->pa_dmat;
88 dev->dmat = dev->bus_dmat;
89 dev->dmat_subregion_p = false;
90 dev->dmat_subregion_min = 0;
91 dev->dmat_subregion_max = __type_max(bus_addr_t);
92
93 /* Find all the memory maps. */
94 CTASSERT(PCI_NUM_RESOURCES < (SIZE_MAX / sizeof(dev->bus_maps[0])));
95 dev->bus_maps = kmem_zalloc(PCI_NUM_RESOURCES *
96 sizeof(dev->bus_maps[0]), KM_SLEEP);
97 dev->bus_nmaps = PCI_NUM_RESOURCES;
98 for (unit = 0; unit < PCI_NUM_RESOURCES; unit++) {
99 struct drm_bus_map *const bm = &dev->bus_maps[unit];
100 const int reg = PCI_BAR(unit);
101 const pcireg_t type =
102 pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
103
104 /* Reject non-memory mappings. */
105 if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
106 aprint_debug_dev(self, "map %u has non-memory type:"
107 " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
108 continue;
109 }
110
111 /*
112 * If it's a 64-bit mapping, don't interpret the second
113 * half of it as another BAR in the next iteration of
114 * the loop -- move on to the next unit.
115 */
116 if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
117 unit++;
118
119 /* Inquire about it. We'll map it in drm_legacy_ioremap. */
120 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
121 &bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
122 aprint_debug_dev(self, "map %u failed\n", unit);
123 continue;
124 }
125
126 /* Assume since it is a memory mapping it can be linear. */
127 bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
128 }
129
130 /* Set up AGP stuff if requested. */
131 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
132 if (pci_find_capability(dev->pdev, PCI_CAP_ID_AGP))
133 dev->agp = drm_agp_init(dev);
134 if (dev->agp)
135 dev->agp->agp_mtrr = arch_phys_wc_add(dev->agp->base,
136 dev->agp->agp_info.aki_info.ai_aperture_size);
137 }
138
139 /* Register the DRM device and do driver-specific initialization. */
140 ret = drm_dev_register(dev, cookie);
141 if (ret)
142 goto fail1;
143
144 /* Success! */
145 *devp = dev;
146 return 0;
147
148 fail2: __unused
149 drm_dev_unregister(dev);
150 fail1: drm_pci_agp_destroy(dev);
151 dev->bus_nmaps = 0;
152 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
153 if (dev->dmat_subregion_p) {
154 bus_dmatag_destroy(dev->dmat);
155 }
156 drm_dev_unref(dev);
157 fail0: return ret;
158 }
159
160 int
161 drm_pci_detach(struct drm_device *dev, int flags __unused)
162 {
163
164 /* Do driver-specific detachment and unregister the device. */
165 drm_dev_unregister(dev);
166
167 /* Tear down AGP stuff if necessary. */
168 drm_pci_agp_destroy(dev);
169
170 /* Free the record of available bus space mappings. */
171 dev->bus_nmaps = 0;
172 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
173
174 /* Tear down bus space and bus DMA tags. */
175 if (dev->dmat_subregion_p) {
176 bus_dmatag_destroy(dev->dmat);
177 }
178
179 drm_dev_unref(dev);
180
181 return 0;
182 }
183
184 void
185 drm_pci_agp_destroy(struct drm_device *dev)
186 {
187
188 if (dev->agp) {
189 arch_phys_wc_del(dev->agp->agp_mtrr);
190 drm_agp_fini(dev);
191 KASSERT(dev->agp == NULL);
192 }
193 }
194
195 int
196 drm_pci_request_irq(struct drm_device *dev, int flags)
197 {
198 const char *const name = device_xname(dev->dev);
199 int (*const handler)(void *) = dev->driver->irq_handler;
200 const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
201 const char *intrstr;
202 char intrbuf[PCI_INTRSTR_LEN];
203 struct drm_bus_irq_cookie *irq_cookie;
204
205 irq_cookie = kmem_alloc(sizeof(*irq_cookie), KM_SLEEP);
206
207 if (dev->pdev->msi_enabled) {
208 if (dev->pdev->pd_intr_handles == NULL) {
209 if (pci_msi_alloc_exact(pa, &irq_cookie->intr_handles,
210 1)) {
211 aprint_error_dev(dev->dev,
212 "couldn't allocate MSI (%s)\n", name);
213 goto error;
214 }
215 } else {
216 irq_cookie->intr_handles = dev->pdev->pd_intr_handles;
217 dev->pdev->pd_intr_handles = NULL;
218 }
219 } else {
220 if (pci_intx_alloc(pa, &irq_cookie->intr_handles)) {
221 aprint_error_dev(dev->dev,
222 "couldn't allocate INTx interrupt (%s)\n", name);
223 goto error;
224 }
225 }
226
227 intrstr = pci_intr_string(pa->pa_pc, irq_cookie->intr_handles[0],
228 intrbuf, sizeof(intrbuf));
229 irq_cookie->ih_cookie = pci_intr_establish_xname(pa->pa_pc,
230 irq_cookie->intr_handles[0], IPL_DRM, handler, dev, name);
231 if (irq_cookie->ih_cookie == NULL) {
232 aprint_error_dev(dev->dev,
233 "couldn't establish interrupt at %s (%s)\n", intrstr, name);
234 pci_intr_release(pa->pa_pc, irq_cookie->intr_handles, 1);
235 goto error;
236 }
237
238 aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n", intrstr, name);
239 dev->irq_cookie = irq_cookie;
240 return 0;
241
242 error:
243 kmem_free(irq_cookie, sizeof(*irq_cookie));
244 return -ENOENT;
245 }
246
247 void
248 drm_pci_free_irq(struct drm_device *dev)
249 {
250 struct drm_bus_irq_cookie *const cookie = dev->irq_cookie;
251 const struct pci_attach_args *pa = drm_pci_attach_args(dev);
252
253 pci_intr_disestablish(pa->pa_pc, cookie->ih_cookie);
254 pci_intr_release(pa->pa_pc, cookie->intr_handles, 1);
255 kmem_free(cookie, sizeof(*cookie));
256 dev->irq_cookie = NULL;
257 }
258
259 int
260 drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
261 {
262 const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
263
264 master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
265 device_unit(device_parent(dev->dev)),
266 pa->pa_bus, pa->pa_device, pa->pa_function);
267 if (master->unique == NULL)
268 return -ENOMEM;
269 master->unique_len = strlen(master->unique);
270
271 return 0;
272 }
273
274 int
275 drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
276 struct drm_unique *unique)
277 {
278 char kbuf[64], ubuf[64];
279 int ret;
280
281 /* Reject excessively long unique strings. */
282 if (unique->unique_len > sizeof(ubuf) - 1)
283 return -EINVAL;
284
285 /* Copy in the alleged unique string, NUL-terminated. */
286 ret = -copyin(unique->unique, ubuf, unique->unique_len);
287 if (ret)
288 return ret;
289 ubuf[unique->unique_len] = '\0';
290
291 /* Make sure it matches what we expect. */
292 snprintf(kbuf, sizeof kbuf, "PCI:%d:%ld:%ld", dev->pdev->bus->number,
293 (long)PCI_SLOT(dev->pdev->devfn),
294 (long)PCI_FUNC(dev->pdev->devfn));
295 if (strncmp(kbuf, ubuf, sizeof(kbuf)) != 0)
296 return -EINVAL;
297
298 /* Remember it. */
299 master->unique = kstrdup(ubuf, GFP_KERNEL);
300 master->unique_len = strlen(master->unique);
301
302 /* Success! */
303 return 0;
304 }
305
306 int
307 drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask)
308 {
309
310 return -ENOSYS;
311 }
312