1/* $NetBSD: nouveau_nvkm_subdev_pci_agp.c,v 1.4 2021/12/18 23:45:41 riastradh Exp $ */ 2 3/* 4 * Copyright 2015 Nouveau Project 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 */ 24#include <sys/cdefs.h> 25__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_pci_agp.c,v 1.4 2021/12/18 23:45:41 riastradh Exp $"); 26 27#include "agp.h" 28#ifdef __NVKM_PCI_AGP_H__ 29#include <core/option.h> 30 31struct nvkm_device_agp_quirk { 32 u16 hostbridge_vendor; 33 u16 hostbridge_device; 34 u16 chip_vendor; 35 u16 chip_device; 36 int mode; 37}; 38 39static const struct nvkm_device_agp_quirk 40nvkm_device_agp_quirks[] = { 41 /* VIA Apollo PRO133x / GeForce FX 5600 Ultra - fdo#20341 */ 42 { PCI_VENDOR_ID_VIA, 0x0691, PCI_VENDOR_ID_NVIDIA, 0x0311, 2 }, 43 /* SiS 761 does not support AGP cards, use PCI mode */ 44 { PCI_VENDOR_ID_SI, 0x0761, PCI_ANY_ID, PCI_ANY_ID, 0 }, 45 {}, 46}; 47 48void 49nvkm_agp_fini(struct nvkm_pci *pci) 50{ 51 if (pci->agp.acquired) { 52 agp_backend_release(pci->agp.bridge); 53 pci->agp.acquired = false; 54 } 55} 56 57/* Ensure AGP controller is in a consistent state in case we need to 58 * execute the VBIOS DEVINIT scripts. 59 */ 60void 61nvkm_agp_preinit(struct nvkm_pci *pci) 62{ 63 struct nvkm_device *device = pci->subdev.device; 64 u32 mode = nvkm_pci_rd32(pci, 0x004c); 65 u32 save[2]; 66 67 /* First of all, disable fast writes, otherwise if it's already 68 * enabled in the AGP bridge and we disable the card's AGP 69 * controller we might be locking ourselves out of it. 70 */ 71 if ((mode | pci->agp.mode) & PCI_AGP_COMMAND_FW) { 72 mode = pci->agp.mode & ~PCI_AGP_COMMAND_FW; 73 agp_enable(pci->agp.bridge, mode); 74 } 75 76 /* clear busmaster bit, and disable AGP */ 77 save[0] = nvkm_pci_rd32(pci, 0x0004); 78 nvkm_pci_wr32(pci, 0x0004, save[0] & ~0x00000004); 79 nvkm_pci_wr32(pci, 0x004c, 0x00000000); 80 81 /* reset PGRAPH, PFIFO and PTIMER */ 82 save[1] = nvkm_mask(device, 0x000200, 0x00011100, 0x00000000); 83 nvkm_mask(device, 0x000200, 0x00011100, save[1]); 84 85 /* and restore busmaster bit (gives effect of resetting AGP) */ 86 nvkm_pci_wr32(pci, 0x0004, save[0]); 87} 88 89int 90nvkm_agp_init(struct nvkm_pci *pci) 91{ 92 if (!agp_backend_acquire(pci->pdev)) { 93 nvkm_error(&pci->subdev, "failed to acquire agp\n"); 94 return -ENODEV; 95 } 96 97 agp_enable(pci->agp.bridge, pci->agp.mode); 98 pci->agp.acquired = true; 99 return 0; 100} 101 102void 103nvkm_agp_dtor(struct nvkm_pci *pci) 104{ 105 arch_phys_wc_del(pci->agp.mtrr); 106#ifdef __NetBSD__ 107 pmap_pv_untrack(pci->agp.base, pci->agp.size); 108#endif 109} 110 111void 112nvkm_agp_ctor(struct nvkm_pci *pci) 113{ 114 const struct nvkm_device_agp_quirk *quirk = nvkm_device_agp_quirks; 115 struct nvkm_subdev *subdev = &pci->subdev; 116 struct nvkm_device *device = subdev->device; 117 struct agp_kern_info info; 118 int mode = -1; 119 120#ifdef __powerpc__ 121 /* Disable AGP by default on all PowerPC machines for now -- At 122 * least some UniNorth-2 AGP bridges are known to be broken: 123 * DMA from the host to the card works just fine, but writeback 124 * from the card to the host goes straight to memory 125 * untranslated bypassing that GATT somehow, making them quite 126 * painful to deal with... 127 */ 128 mode = 0; 129#endif 130 mode = nvkm_longopt(device->cfgopt, "NvAGP", mode); 131 132 /* acquire bridge temporarily, so that we can copy its info */ 133 if (!(pci->agp.bridge = agp_backend_acquire(pci->pdev))) { 134 nvkm_warn(subdev, "failed to acquire agp\n"); 135 return; 136 } 137 agp_copy_info(pci->agp.bridge, &info); 138 agp_backend_release(pci->agp.bridge); 139 140#ifdef __NetBSD__ 141 pci->agp.mode = info.aki_info.ai_mode; 142 pci->agp.base = info.aki_info.ai_aperture_base; 143 pci->agp.size = info.aki_info.ai_aperture_size; 144 pci->agp.cma = !info.aki_info.ai_memory_allowed; /* XXX ? */ 145#else 146 pci->agp.mode = info.mode; 147 pci->agp.base = info.aper_base; 148 pci->agp.size = info.aper_size * 1024 * 1024; 149 pci->agp.cma = info.cant_use_aperture; 150#endif 151 pci->agp.mtrr = -1; 152 153 /* determine if bridge + chipset combination needs a workaround */ 154 while (quirk->hostbridge_vendor) { 155#ifdef __NetBSD__ 156 uint16_t vendor = PCI_VENDOR(info.aki_info.ai_devid); 157 uint16_t product = PCI_PRODUCT(info.aki_info.ai_devid); 158#else 159 uint16_t vendor = info.device->vendor; 160 uint16_t product = info.device->device; 161#endif 162 if (vendor == quirk->hostbridge_vendor && 163 product == quirk->hostbridge_device && 164 (quirk->chip_vendor == (u16)PCI_ANY_ID || 165 pci->pdev->vendor == quirk->chip_vendor) && 166 (quirk->chip_device == (u16)PCI_ANY_ID || 167 pci->pdev->device == quirk->chip_device)) { 168 nvkm_info(subdev, "forcing default agp mode to %dX, " 169 "use NvAGP=<mode> to override\n", 170 quirk->mode); 171 mode = quirk->mode; 172 break; 173 } 174 quirk++; 175 } 176 177 /* apply quirk / user-specified mode */ 178 if (mode >= 1) { 179 if (pci->agp.mode & 0x00000008) 180 mode /= 4; /* AGPv3 */ 181 pci->agp.mode &= ~0x00000007; 182 pci->agp.mode |= (mode & 0x7); 183 } else 184 if (mode == 0) { 185 pci->agp.bridge = NULL; 186 return; 187 } 188 189 /* fast writes appear to be broken on nv18, they make the card 190 * lock up randomly. 191 */ 192 if (device->chipset == 0x18) 193 pci->agp.mode &= ~PCI_AGP_COMMAND_FW; 194 195#ifdef __NetBSD__ 196 pmap_pv_track(pci->agp.base, pci->agp.size); 197#endif 198 pci->agp.mtrr = arch_phys_wc_add(pci->agp.base, pci->agp.size); 199} 200#endif 201