1 /* 2 * Copyright 2012 Red Hat Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: Ben Skeggs 23 */ 24 #include "priv.h" 25 26 #include <core/option.h> 27 #include <subdev/top.h> 28 29 void 30 nvkm_mc_unk260(struct nvkm_device *device, u32 data) 31 { 32 struct nvkm_mc *mc = device->mc; 33 if (likely(mc) && mc->func->unk260) 34 mc->func->unk260(mc, data); 35 } 36 37 void 38 nvkm_mc_intr_mask(struct nvkm_device *device, enum nvkm_subdev_type type, int inst, bool en) 39 { 40 struct nvkm_subdev *subdev = nvkm_device_subdev(device, type, inst); 41 42 if (subdev) { 43 if (en) 44 nvkm_intr_allow(subdev, NVKM_INTR_SUBDEV); 45 else 46 nvkm_intr_block(subdev, NVKM_INTR_SUBDEV); 47 } 48 } 49 50 static u32 51 nvkm_mc_reset_mask(struct nvkm_device *device, bool isauto, enum nvkm_subdev_type type, int inst) 52 { 53 struct nvkm_mc *mc = device->mc; 54 const struct nvkm_mc_map *map; 55 u64 pmc_enable = 0; 56 if (likely(mc)) { 57 if (!(pmc_enable = nvkm_top_reset(device, type, inst))) { 58 for (map = mc->func->reset; map && map->stat; map++) { 59 if (!isauto || !map->noauto) { 60 if (map->type == type && map->inst == inst) { 61 pmc_enable = map->stat; 62 break; 63 } 64 } 65 } 66 } 67 } 68 return pmc_enable; 69 } 70 71 void 72 nvkm_mc_reset(struct nvkm_device *device, enum nvkm_subdev_type type, int inst) 73 { 74 u64 pmc_enable = nvkm_mc_reset_mask(device, true, type, inst); 75 if (pmc_enable) { 76 nvkm_mask(device, 0x000200, pmc_enable, 0x00000000); 77 nvkm_mask(device, 0x000200, pmc_enable, pmc_enable); 78 nvkm_rd32(device, 0x000200); 79 } 80 } 81 82 void 83 nvkm_mc_disable(struct nvkm_device *device, enum nvkm_subdev_type type, int inst) 84 { 85 u64 pmc_enable = nvkm_mc_reset_mask(device, false, type, inst); 86 if (pmc_enable) 87 nvkm_mask(device, 0x000200, pmc_enable, 0x00000000); 88 } 89 90 void 91 nvkm_mc_enable(struct nvkm_device *device, enum nvkm_subdev_type type, int inst) 92 { 93 u64 pmc_enable = nvkm_mc_reset_mask(device, false, type, inst); 94 if (pmc_enable) { 95 nvkm_mask(device, 0x000200, pmc_enable, pmc_enable); 96 nvkm_rd32(device, 0x000200); 97 } 98 } 99 100 bool 101 nvkm_mc_enabled(struct nvkm_device *device, enum nvkm_subdev_type type, int inst) 102 { 103 u64 pmc_enable = nvkm_mc_reset_mask(device, false, type, inst); 104 105 return (pmc_enable != 0) && 106 ((nvkm_rd32(device, 0x000200) & pmc_enable) == pmc_enable); 107 } 108 109 110 static int 111 nvkm_mc_init(struct nvkm_subdev *subdev) 112 { 113 struct nvkm_mc *mc = nvkm_mc(subdev); 114 if (mc->func->init) 115 mc->func->init(mc); 116 return 0; 117 } 118 119 static void * 120 nvkm_mc_dtor(struct nvkm_subdev *subdev) 121 { 122 return nvkm_mc(subdev); 123 } 124 125 static const struct nvkm_subdev_func 126 nvkm_mc = { 127 .dtor = nvkm_mc_dtor, 128 .init = nvkm_mc_init, 129 }; 130 131 int 132 nvkm_mc_new_(const struct nvkm_mc_func *func, struct nvkm_device *device, 133 enum nvkm_subdev_type type, int inst, struct nvkm_mc **pmc) 134 { 135 struct nvkm_mc *mc; 136 int ret; 137 138 if (!(mc = *pmc = kzalloc(sizeof(*mc), GFP_KERNEL))) 139 return -ENOMEM; 140 141 nvkm_subdev_ctor(&nvkm_mc, device, type, inst, &mc->subdev); 142 mc->func = func; 143 144 if (mc->func->intr) { 145 ret = nvkm_intr_add(mc->func->intr, mc->func->intrs, &mc->subdev, 146 mc->func->intr_nonstall ? 2 : 1, &mc->intr); 147 if (ret) 148 return ret; 149 } 150 151 return 0; 152 } 153