1 /* 2 * Copyright 2013 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 #include "conn.h" 26 #include "head.h" 27 #include "ior.h" 28 #include "outp.h" 29 30 #include <core/client.h> 31 #include <core/ramht.h> 32 33 #include <nvif/class.h> 34 #include <nvif/cl0046.h> 35 #include <nvif/event.h> 36 #include <nvif/unpack.h> 37 38 static void 39 nvkm_disp_vblank_fini(struct nvkm_event *event, int type, int id) 40 { 41 struct nvkm_disp *disp = container_of(event, typeof(*disp), vblank); 42 struct nvkm_head *head = nvkm_head_find(disp, id); 43 if (head) 44 head->func->vblank_put(head); 45 } 46 47 static void 48 nvkm_disp_vblank_init(struct nvkm_event *event, int type, int id) 49 { 50 struct nvkm_disp *disp = container_of(event, typeof(*disp), vblank); 51 struct nvkm_head *head = nvkm_head_find(disp, id); 52 if (head) 53 head->func->vblank_get(head); 54 } 55 56 static const struct nvkm_event_func 57 nvkm_disp_vblank_func = { 58 .init = nvkm_disp_vblank_init, 59 .fini = nvkm_disp_vblank_fini, 60 }; 61 62 void 63 nvkm_disp_vblank(struct nvkm_disp *disp, int head) 64 { 65 nvkm_event_ntfy(&disp->vblank, head, NVKM_DISP_HEAD_EVENT_VBLANK); 66 } 67 68 static int 69 nvkm_disp_class_new(struct nvkm_device *device, 70 const struct nvkm_oclass *oclass, void *data, u32 size, 71 struct nvkm_object **pobject) 72 { 73 return nvkm_udisp_new(oclass, data, size, pobject); 74 } 75 76 static const struct nvkm_device_oclass 77 nvkm_disp_sclass = { 78 .ctor = nvkm_disp_class_new, 79 }; 80 81 static int 82 nvkm_disp_class_get(struct nvkm_oclass *oclass, int index, 83 const struct nvkm_device_oclass **class) 84 { 85 struct nvkm_disp *disp = nvkm_disp(oclass->engine); 86 if (index == 0) { 87 oclass->base = disp->func->root; 88 *class = &nvkm_disp_sclass; 89 return 0; 90 } 91 return 1; 92 } 93 94 static void 95 nvkm_disp_intr(struct nvkm_engine *engine) 96 { 97 struct nvkm_disp *disp = nvkm_disp(engine); 98 disp->func->intr(disp); 99 } 100 101 static int 102 nvkm_disp_fini(struct nvkm_engine *engine, bool suspend) 103 { 104 struct nvkm_disp *disp = nvkm_disp(engine); 105 struct nvkm_outp *outp; 106 107 if (disp->func->fini) 108 disp->func->fini(disp); 109 110 list_for_each_entry(outp, &disp->outps, head) { 111 if (outp->func->fini) 112 outp->func->fini(outp); 113 } 114 115 return 0; 116 } 117 118 static int 119 nvkm_disp_init(struct nvkm_engine *engine) 120 { 121 struct nvkm_disp *disp = nvkm_disp(engine); 122 struct nvkm_outp *outp; 123 struct nvkm_ior *ior; 124 125 list_for_each_entry(outp, &disp->outps, head) { 126 if (outp->func->init) 127 outp->func->init(outp); 128 } 129 130 if (disp->func->init) { 131 int ret = disp->func->init(disp); 132 if (ret) 133 return ret; 134 } 135 136 /* Set 'normal' (ie. when it's attached to a head) state for 137 * each output resource to 'fully enabled'. 138 */ 139 list_for_each_entry(ior, &disp->iors, head) { 140 ior->func->power(ior, true, true, true, true, true); 141 } 142 143 return 0; 144 } 145 146 static int 147 nvkm_disp_oneinit(struct nvkm_engine *engine) 148 { 149 struct nvkm_disp *disp = nvkm_disp(engine); 150 struct nvkm_subdev *subdev = &disp->engine.subdev; 151 struct nvkm_head *head; 152 int ret, i; 153 154 if (disp->func->oneinit) { 155 ret = disp->func->oneinit(disp); 156 if (ret) 157 return ret; 158 } 159 160 i = 0; 161 list_for_each_entry(head, &disp->heads, head) 162 i = max(i, head->id + 1); 163 164 return nvkm_event_init(&nvkm_disp_vblank_func, subdev, 1, i, &disp->vblank); 165 } 166 167 static void * 168 nvkm_disp_dtor(struct nvkm_engine *engine) 169 { 170 struct nvkm_disp *disp = nvkm_disp(engine); 171 struct nvkm_conn *conn; 172 struct nvkm_outp *outp; 173 struct nvkm_ior *ior; 174 struct nvkm_head *head; 175 void *data = disp; 176 177 nvkm_ramht_del(&disp->ramht); 178 nvkm_gpuobj_del(&disp->inst); 179 180 nvkm_event_fini(&disp->uevent); 181 182 if (disp->super.wq) { 183 destroy_workqueue(disp->super.wq); 184 mutex_destroy(&disp->super.mutex); 185 } 186 187 nvkm_event_fini(&disp->vblank); 188 189 while (!list_empty(&disp->conns)) { 190 conn = list_first_entry(&disp->conns, typeof(*conn), head); 191 list_del(&conn->head); 192 nvkm_conn_del(&conn); 193 } 194 195 while (!list_empty(&disp->outps)) { 196 outp = list_first_entry(&disp->outps, typeof(*outp), head); 197 list_del(&outp->head); 198 nvkm_outp_del(&outp); 199 } 200 201 while (!list_empty(&disp->iors)) { 202 ior = list_first_entry(&disp->iors, typeof(*ior), head); 203 nvkm_ior_del(&ior); 204 } 205 206 while (!list_empty(&disp->heads)) { 207 head = list_first_entry(&disp->heads, typeof(*head), head); 208 nvkm_head_del(&head); 209 } 210 211 return data; 212 } 213 214 static const struct nvkm_engine_func 215 nvkm_disp = { 216 .dtor = nvkm_disp_dtor, 217 .oneinit = nvkm_disp_oneinit, 218 .init = nvkm_disp_init, 219 .fini = nvkm_disp_fini, 220 .intr = nvkm_disp_intr, 221 .base.sclass = nvkm_disp_class_get, 222 }; 223 224 int 225 nvkm_disp_new_(const struct nvkm_disp_func *func, struct nvkm_device *device, 226 enum nvkm_subdev_type type, int inst, struct nvkm_disp **pdisp) 227 { 228 struct nvkm_disp *disp; 229 int ret; 230 231 if (!(disp = *pdisp = kzalloc(sizeof(**pdisp), GFP_KERNEL))) 232 return -ENOMEM; 233 234 disp->func = func; 235 INIT_LIST_HEAD(&disp->heads); 236 INIT_LIST_HEAD(&disp->iors); 237 INIT_LIST_HEAD(&disp->outps); 238 INIT_LIST_HEAD(&disp->conns); 239 spin_lock_init(&disp->client.lock); 240 241 ret = nvkm_engine_ctor(&nvkm_disp, device, type, inst, true, &disp->engine); 242 if (ret) 243 return ret; 244 245 if (func->super) { 246 disp->super.wq = create_singlethread_workqueue("nvkm-disp"); 247 if (!disp->super.wq) 248 return -ENOMEM; 249 250 INIT_WORK(&disp->super.work, func->super); 251 mutex_init(&disp->super.mutex); 252 } 253 254 return nvkm_event_init(func->uevent, &disp->engine.subdev, 1, ARRAY_SIZE(disp->chan), 255 &disp->uevent); 256 } 257