1 /* $NetBSD: drm_pci.c,v 1.47 2021/12/19 11:54:32 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.47 2021/12/19 11:54:32 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, struct pci_dev *pdev) 63 { 64 device_t self = dev->dev; 65 const struct pci_attach_args *pa = &pdev->pd_pa; 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 if (dev->agp) { 144 arch_phys_wc_del(dev->agp->agp_mtrr); 145 drm_agp_fini(dev); 146 KASSERT(dev->agp == NULL); 147 } 148 149 /* Free the record of available bus space mappings. */ 150 dev->bus_nmaps = 0; 151 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0])); 152 153 /* Tear down bus space and bus DMA tags. */ 154 if (dev->dmat_subregion_p) { 155 bus_dmatag_destroy(dev->dmat); 156 } 157 } 158 159 int 160 drm_pci_request_irq(struct drm_device *dev, int flags) 161 { 162 const char *const name = device_xname(dev->dev); 163 int (*const handler)(void *) = dev->driver->irq_handler; 164 const struct pci_attach_args *const pa = drm_pci_attach_args(dev); 165 const char *intrstr; 166 char intrbuf[PCI_INTRSTR_LEN]; 167 struct drm_bus_irq_cookie *irq_cookie; 168 169 irq_cookie = kmem_alloc(sizeof(*irq_cookie), KM_SLEEP); 170 171 if (dev->pdev->msi_enabled) { 172 if (dev->pdev->pd_intr_handles == NULL) { 173 if (pci_msi_alloc_exact(pa, &irq_cookie->intr_handles, 174 1)) { 175 aprint_error_dev(dev->dev, 176 "couldn't allocate MSI (%s)\n", name); 177 goto error; 178 } 179 } else { 180 irq_cookie->intr_handles = dev->pdev->pd_intr_handles; 181 dev->pdev->pd_intr_handles = NULL; 182 } 183 } else { 184 if (pci_intx_alloc(pa, &irq_cookie->intr_handles)) { 185 aprint_error_dev(dev->dev, 186 "couldn't allocate INTx interrupt (%s)\n", name); 187 goto error; 188 } 189 } 190 191 intrstr = pci_intr_string(pa->pa_pc, irq_cookie->intr_handles[0], 192 intrbuf, sizeof(intrbuf)); 193 irq_cookie->ih_cookie = pci_intr_establish_xname(pa->pa_pc, 194 irq_cookie->intr_handles[0], IPL_DRM, handler, dev, name); 195 if (irq_cookie->ih_cookie == NULL) { 196 aprint_error_dev(dev->dev, 197 "couldn't establish interrupt at %s (%s)\n", intrstr, name); 198 pci_intr_release(pa->pa_pc, irq_cookie->intr_handles, 1); 199 goto error; 200 } 201 202 aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n", intrstr, name); 203 dev->irq_cookie = irq_cookie; 204 return 0; 205 206 error: 207 kmem_free(irq_cookie, sizeof(*irq_cookie)); 208 return -ENOENT; 209 } 210 211 void 212 drm_pci_free_irq(struct drm_device *dev) 213 { 214 struct drm_bus_irq_cookie *const cookie = dev->irq_cookie; 215 const struct pci_attach_args *pa = drm_pci_attach_args(dev); 216 217 pci_intr_disestablish(pa->pa_pc, cookie->ih_cookie); 218 pci_intr_release(pa->pa_pc, cookie->intr_handles, 1); 219 kmem_free(cookie, sizeof(*cookie)); 220 dev->irq_cookie = NULL; 221 } 222