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