drm_pci.c revision 1.41 1 /* $NetBSD: drm_pci.c,v 1.41 2021/12/19 10:33:00 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.41 2021/12/19 10:33:00 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/drm_device.h>
42 #include <drm/drm_drv.h>
43 #include <drm/drm_legacy.h>
44 #include <drm/drm_pci.h>
45
46 #include "../dist/drm/drm_internal.h"
47
48 struct drm_bus_irq_cookie {
49 pci_intr_handle_t *intr_handles;
50 void *ih_cookie;
51 };
52
53 static const struct pci_attach_args *
54 drm_pci_attach_args(struct drm_device *dev)
55 {
56 return &dev->pdev->pd_pa;
57 }
58
59 int
60 drm_pci_attach(device_t self, const struct pci_attach_args *pa,
61 struct pci_dev *pdev, struct drm_driver *driver, unsigned long cookie,
62 struct drm_device **devp)
63 {
64 struct drm_device *dev;
65 unsigned int unit;
66 int ret;
67
68 /* Ensure the drm agp hooks are initialized. */
69 /* XXX errno NetBSD->Linux */
70 ret = -drm_guarantee_initialized();
71 if (ret)
72 goto fail0;
73
74 /* Create a DRM device. */
75 dev = drm_dev_alloc(driver, self);
76 if (dev == NULL) {
77 ret = -ENOMEM;
78 goto fail0;
79 }
80
81 dev->pdev = pdev;
82
83 /* XXX Set the power state to D0? */
84
85 /* Set up the bus space and bus DMA tags. */
86 dev->bst = pa->pa_memt;
87 dev->bus_dmat = (pci_dma64_available(pa)? pa->pa_dmat64 : pa->pa_dmat);
88 dev->bus_dmat32 = pa->pa_dmat;
89 dev->dmat = dev->bus_dmat;
90 dev->dmat_subregion_p = false;
91 dev->dmat_subregion_min = 0;
92 dev->dmat_subregion_max = __type_max(bus_addr_t);
93
94 /* Find all the memory maps. */
95 CTASSERT(PCI_NUM_RESOURCES < (SIZE_MAX / sizeof(dev->bus_maps[0])));
96 dev->bus_maps = kmem_zalloc(PCI_NUM_RESOURCES *
97 sizeof(dev->bus_maps[0]), KM_SLEEP);
98 dev->bus_nmaps = PCI_NUM_RESOURCES;
99 for (unit = 0; unit < PCI_NUM_RESOURCES; unit++) {
100 struct drm_bus_map *const bm = &dev->bus_maps[unit];
101 const int reg = PCI_BAR(unit);
102 const pcireg_t type =
103 pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
104
105 /* Reject non-memory mappings. */
106 if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
107 aprint_debug_dev(self, "map %u has non-memory type:"
108 " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
109 continue;
110 }
111
112 /*
113 * If it's a 64-bit mapping, don't interpret the second
114 * half of it as another BAR in the next iteration of
115 * the loop -- move on to the next unit.
116 */
117 if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
118 unit++;
119
120 /* Inquire about it. We'll map it in drm_legacy_ioremap. */
121 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
122 &bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
123 aprint_debug_dev(self, "map %u failed\n", unit);
124 continue;
125 }
126
127 /* Assume since it is a memory mapping it can be linear. */
128 bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
129 }
130
131 /* Set up AGP stuff if requested. */
132 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
133 if (pci_find_capability(dev->pdev, PCI_CAP_ID_AGP))
134 dev->agp = drm_agp_init(dev);
135 if (dev->agp)
136 dev->agp->agp_mtrr = arch_phys_wc_add(dev->agp->base,
137 dev->agp->agp_info.aki_info.ai_aperture_size);
138 }
139
140 /* Register the DRM device and do driver-specific initialization. */
141 ret = drm_dev_register(dev, cookie);
142 if (ret)
143 goto fail1;
144
145 /* Success! */
146 *devp = dev;
147 return 0;
148
149 fail2: __unused
150 drm_dev_unregister(dev);
151 fail1: drm_pci_agp_destroy(dev);
152 dev->bus_nmaps = 0;
153 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
154 if (dev->dmat_subregion_p) {
155 bus_dmatag_destroy(dev->dmat);
156 }
157 drm_dev_put(dev);
158 fail0: return ret;
159 }
160
161 int
162 drm_pci_detach(struct drm_device *dev, int flags __unused)
163 {
164
165 /* Do driver-specific detachment and unregister the device. */
166 drm_dev_unregister(dev);
167
168 /* Tear down AGP stuff if necessary. */
169 drm_pci_agp_destroy(dev);
170
171 /* Free the record of available bus space mappings. */
172 dev->bus_nmaps = 0;
173 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
174
175 /* Tear down bus space and bus DMA tags. */
176 if (dev->dmat_subregion_p) {
177 bus_dmatag_destroy(dev->dmat);
178 }
179
180 drm_dev_put(dev);
181
182 return 0;
183 }
184
185 void
186 drm_pci_agp_destroy(struct drm_device *dev)
187 {
188
189 if (dev->agp) {
190 arch_phys_wc_del(dev->agp->agp_mtrr);
191 drm_agp_fini(dev);
192 KASSERT(dev->agp == NULL);
193 }
194 }
195
196 int
197 drm_pci_request_irq(struct drm_device *dev, int flags)
198 {
199 const char *const name = device_xname(dev->dev);
200 int (*const handler)(void *) = dev->driver->irq_handler;
201 const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
202 const char *intrstr;
203 char intrbuf[PCI_INTRSTR_LEN];
204 struct drm_bus_irq_cookie *irq_cookie;
205
206 irq_cookie = kmem_alloc(sizeof(*irq_cookie), KM_SLEEP);
207
208 if (dev->pdev->msi_enabled) {
209 if (dev->pdev->pd_intr_handles == NULL) {
210 if (pci_msi_alloc_exact(pa, &irq_cookie->intr_handles,
211 1)) {
212 aprint_error_dev(dev->dev,
213 "couldn't allocate MSI (%s)\n", name);
214 goto error;
215 }
216 } else {
217 irq_cookie->intr_handles = dev->pdev->pd_intr_handles;
218 dev->pdev->pd_intr_handles = NULL;
219 }
220 } else {
221 if (pci_intx_alloc(pa, &irq_cookie->intr_handles)) {
222 aprint_error_dev(dev->dev,
223 "couldn't allocate INTx interrupt (%s)\n", name);
224 goto error;
225 }
226 }
227
228 intrstr = pci_intr_string(pa->pa_pc, irq_cookie->intr_handles[0],
229 intrbuf, sizeof(intrbuf));
230 irq_cookie->ih_cookie = pci_intr_establish_xname(pa->pa_pc,
231 irq_cookie->intr_handles[0], IPL_DRM, handler, dev, name);
232 if (irq_cookie->ih_cookie == NULL) {
233 aprint_error_dev(dev->dev,
234 "couldn't establish interrupt at %s (%s)\n", intrstr, name);
235 pci_intr_release(pa->pa_pc, irq_cookie->intr_handles, 1);
236 goto error;
237 }
238
239 aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n", intrstr, name);
240 dev->irq_cookie = irq_cookie;
241 return 0;
242
243 error:
244 kmem_free(irq_cookie, sizeof(*irq_cookie));
245 return -ENOENT;
246 }
247
248 void
249 drm_pci_free_irq(struct drm_device *dev)
250 {
251 struct drm_bus_irq_cookie *const cookie = dev->irq_cookie;
252 const struct pci_attach_args *pa = drm_pci_attach_args(dev);
253
254 pci_intr_disestablish(pa->pa_pc, cookie->ih_cookie);
255 pci_intr_release(pa->pa_pc, cookie->intr_handles, 1);
256 kmem_free(cookie, sizeof(*cookie));
257 dev->irq_cookie = NULL;
258 }
259
260 int
261 drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
262 {
263 const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
264
265 master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
266 device_unit(device_parent(dev->dev)),
267 pa->pa_bus, pa->pa_device, pa->pa_function);
268 if (master->unique == NULL)
269 return -ENOMEM;
270 master->unique_len = strlen(master->unique);
271
272 return 0;
273 }
274
275 int
276 drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
277 struct drm_unique *unique)
278 {
279 char kbuf[64], ubuf[64];
280 int ret;
281
282 /* Reject excessively long unique strings. */
283 if (unique->unique_len > sizeof(ubuf) - 1)
284 return -EINVAL;
285
286 /* Copy in the alleged unique string, NUL-terminated. */
287 ret = -copyin(unique->unique, ubuf, unique->unique_len);
288 if (ret)
289 return ret;
290 ubuf[unique->unique_len] = '\0';
291
292 /* Make sure it matches what we expect. */
293 snprintf(kbuf, sizeof kbuf, "PCI:%d:%ld:%ld", dev->pdev->bus->number,
294 (long)PCI_SLOT(dev->pdev->devfn),
295 (long)PCI_FUNC(dev->pdev->devfn));
296 if (strncmp(kbuf, ubuf, sizeof(kbuf)) != 0)
297 return -EINVAL;
298
299 /* Remember it. */
300 master->unique = kstrdup(ubuf, GFP_KERNEL);
301 master->unique_len = strlen(master->unique);
302
303 /* Success! */
304 return 0;
305 }
306