1 /* $NetBSD: nouveau_nvkm_subdev_devinit_gm200.c,v 1.2 2021/12/18 23:45:39 riastradh Exp $ */ 2 3 /* 4 * Copyright 2013 Red Hat Inc. 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 * Authors: Ben Skeggs 25 */ 26 #include <sys/cdefs.h> 27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_devinit_gm200.c,v 1.2 2021/12/18 23:45:39 riastradh Exp $"); 28 29 #include "nv50.h" 30 31 #include <subdev/bios.h> 32 #include <subdev/bios/bit.h> 33 #include <subdev/bios/pmu.h> 34 #include <subdev/timer.h> 35 36 static void 37 pmu_code(struct nv50_devinit *init, u32 pmu, u32 img, u32 len, bool sec) 38 { 39 struct nvkm_device *device = init->base.subdev.device; 40 struct nvkm_bios *bios = device->bios; 41 int i; 42 43 nvkm_wr32(device, 0x10a180, 0x01000000 | (sec ? 0x10000000 : 0) | pmu); 44 for (i = 0; i < len; i += 4) { 45 if ((i & 0xff) == 0) 46 nvkm_wr32(device, 0x10a188, (pmu + i) >> 8); 47 nvkm_wr32(device, 0x10a184, nvbios_rd32(bios, img + i)); 48 } 49 50 while (i & 0xff) { 51 nvkm_wr32(device, 0x10a184, 0x00000000); 52 i += 4; 53 } 54 } 55 56 static void 57 pmu_data(struct nv50_devinit *init, u32 pmu, u32 img, u32 len) 58 { 59 struct nvkm_device *device = init->base.subdev.device; 60 struct nvkm_bios *bios = device->bios; 61 int i; 62 63 nvkm_wr32(device, 0x10a1c0, 0x01000000 | pmu); 64 for (i = 0; i < len; i += 4) 65 nvkm_wr32(device, 0x10a1c4, nvbios_rd32(bios, img + i)); 66 } 67 68 static u32 69 pmu_args(struct nv50_devinit *init, u32 argp, u32 argi) 70 { 71 struct nvkm_device *device = init->base.subdev.device; 72 nvkm_wr32(device, 0x10a1c0, argp); 73 nvkm_wr32(device, 0x10a1c0, nvkm_rd32(device, 0x10a1c4) + argi); 74 return nvkm_rd32(device, 0x10a1c4); 75 } 76 77 static void 78 pmu_exec(struct nv50_devinit *init, u32 init_addr) 79 { 80 struct nvkm_device *device = init->base.subdev.device; 81 nvkm_wr32(device, 0x10a104, init_addr); 82 nvkm_wr32(device, 0x10a10c, 0x00000000); 83 nvkm_wr32(device, 0x10a100, 0x00000002); 84 } 85 86 static int 87 pmu_load(struct nv50_devinit *init, u8 type, bool post, 88 u32 *init_addr_pmu, u32 *args_addr_pmu) 89 { 90 struct nvkm_subdev *subdev = &init->base.subdev; 91 struct nvkm_bios *bios = subdev->device->bios; 92 struct nvbios_pmuR pmu; 93 94 if (!nvbios_pmuRm(bios, type, &pmu)) 95 return -EINVAL; 96 97 if (!post) 98 return 0; 99 100 pmu_code(init, pmu.boot_addr_pmu, pmu.boot_addr, pmu.boot_size, false); 101 pmu_code(init, pmu.code_addr_pmu, pmu.code_addr, pmu.code_size, true); 102 pmu_data(init, pmu.data_addr_pmu, pmu.data_addr, pmu.data_size); 103 104 if (init_addr_pmu) { 105 *init_addr_pmu = pmu.init_addr_pmu; 106 *args_addr_pmu = pmu.args_addr_pmu; 107 return 0; 108 } 109 110 return pmu_exec(init, pmu.init_addr_pmu), 0; 111 } 112 113 void 114 gm200_devinit_preos(struct nv50_devinit *init, bool post) 115 { 116 /* Optional: Execute PRE_OS application on PMU, which should at 117 * least take care of fans until a full PMU has been loaded. 118 */ 119 pmu_load(init, 0x01, post, NULL, NULL); 120 } 121 122 int 123 gm200_devinit_post(struct nvkm_devinit *base, bool post) 124 { 125 struct nv50_devinit *init = nv50_devinit(base); 126 struct nvkm_subdev *subdev = &init->base.subdev; 127 struct nvkm_device *device = subdev->device; 128 struct nvkm_bios *bios = device->bios; 129 struct bit_entry bit_I; 130 u32 exec, args; 131 int ret; 132 133 if (bit_entry(bios, 'I', &bit_I) || bit_I.version != 1 || 134 bit_I.length < 0x1c) { 135 nvkm_error(subdev, "VBIOS PMU init data not found\n"); 136 return -EINVAL; 137 } 138 139 /* Upload DEVINIT application from VBIOS onto PMU. */ 140 ret = pmu_load(init, 0x04, post, &exec, &args); 141 if (ret) { 142 nvkm_error(subdev, "VBIOS PMU/DEVINIT not found\n"); 143 return ret; 144 } 145 146 /* Upload tables required by opcodes in boot scripts. */ 147 if (post) { 148 u32 pmu = pmu_args(init, args + 0x08, 0x08); 149 u32 img = nvbios_rd16(bios, bit_I.offset + 0x14); 150 u32 len = nvbios_rd16(bios, bit_I.offset + 0x16); 151 pmu_data(init, pmu, img, len); 152 } 153 154 /* Upload boot scripts. */ 155 if (post) { 156 u32 pmu = pmu_args(init, args + 0x08, 0x10); 157 u32 img = nvbios_rd16(bios, bit_I.offset + 0x18); 158 u32 len = nvbios_rd16(bios, bit_I.offset + 0x1a); 159 pmu_data(init, pmu, img, len); 160 } 161 162 /* Execute DEVINIT. */ 163 if (post) { 164 nvkm_wr32(device, 0x10a040, 0x00005000); 165 pmu_exec(init, exec); 166 if (nvkm_msec(device, 2000, 167 if (nvkm_rd32(device, 0x10a040) & 0x00002000) 168 break; 169 ) < 0) 170 return -ETIMEDOUT; 171 } 172 173 gm200_devinit_preos(init, post); 174 return 0; 175 } 176 177 static const struct nvkm_devinit_func 178 gm200_devinit = { 179 .preinit = gf100_devinit_preinit, 180 .init = nv50_devinit_init, 181 .post = gm200_devinit_post, 182 .pll_set = gf100_devinit_pll_set, 183 .disable = gm107_devinit_disable, 184 }; 185 186 int 187 gm200_devinit_new(struct nvkm_device *device, int index, 188 struct nvkm_devinit **pinit) 189 { 190 return nv50_devinit_new_(&gm200_devinit, device, index, pinit); 191 } 192