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