1 /* 2 * Copyright 2018 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 #include "wndw.h" 23 24 #include <nvif/class.h> 25 #include <nvif/cl0002.h> 26 27 #include <drm/drm_atomic_helper.h> 28 #include "nouveau_bo.h" 29 30 static void 31 nv50_wndw_ctxdma_del(struct nv50_wndw_ctxdma *ctxdma) 32 { 33 nvif_object_fini(&ctxdma->object); 34 list_del(&ctxdma->head); 35 kfree(ctxdma); 36 } 37 38 static struct nv50_wndw_ctxdma * 39 nv50_wndw_ctxdma_new(struct nv50_wndw *wndw, struct nouveau_framebuffer *fb) 40 { 41 struct nouveau_drm *drm = nouveau_drm(fb->base.dev); 42 struct nv50_wndw_ctxdma *ctxdma; 43 const u8 kind = fb->nvbo->kind; 44 const u32 handle = 0xfb000000 | kind; 45 struct { 46 struct nv_dma_v0 base; 47 union { 48 struct nv50_dma_v0 nv50; 49 struct gf100_dma_v0 gf100; 50 struct gf119_dma_v0 gf119; 51 }; 52 } args = {}; 53 u32 argc = sizeof(args.base); 54 int ret; 55 56 list_for_each_entry(ctxdma, &wndw->ctxdma.list, head) { 57 if (ctxdma->object.handle == handle) 58 return ctxdma; 59 } 60 61 if (!(ctxdma = kzalloc(sizeof(*ctxdma), GFP_KERNEL))) 62 return ERR_PTR(-ENOMEM); 63 list_add(&ctxdma->head, &wndw->ctxdma.list); 64 65 args.base.target = NV_DMA_V0_TARGET_VRAM; 66 args.base.access = NV_DMA_V0_ACCESS_RDWR; 67 args.base.start = 0; 68 args.base.limit = drm->client.device.info.ram_user - 1; 69 70 if (drm->client.device.info.chipset < 0x80) { 71 args.nv50.part = NV50_DMA_V0_PART_256; 72 argc += sizeof(args.nv50); 73 } else 74 if (drm->client.device.info.chipset < 0xc0) { 75 args.nv50.part = NV50_DMA_V0_PART_256; 76 args.nv50.kind = kind; 77 argc += sizeof(args.nv50); 78 } else 79 if (drm->client.device.info.chipset < 0xd0) { 80 args.gf100.kind = kind; 81 argc += sizeof(args.gf100); 82 } else { 83 args.gf119.page = GF119_DMA_V0_PAGE_LP; 84 args.gf119.kind = kind; 85 argc += sizeof(args.gf119); 86 } 87 88 ret = nvif_object_init(wndw->ctxdma.parent, handle, NV_DMA_IN_MEMORY, 89 &args, argc, &ctxdma->object); 90 if (ret) { 91 nv50_wndw_ctxdma_del(ctxdma); 92 return ERR_PTR(ret); 93 } 94 95 return ctxdma; 96 } 97 98 int 99 nv50_wndw_wait_armed(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw) 100 { 101 if (asyw->set.ntfy) 102 return wndw->func->ntfy_wait_begun(wndw, asyw); 103 return 0; 104 } 105 106 u32 107 nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 interlock, bool flush, 108 struct nv50_wndw_atom *asyw) 109 { 110 if (asyw->clr.sema && (!asyw->set.sema || flush)) 111 wndw->func->sema_clr(wndw); 112 if (asyw->clr.ntfy && (!asyw->set.ntfy || flush)) 113 wndw->func->ntfy_clr(wndw); 114 if (asyw->clr.image && (!asyw->set.image || flush)) 115 wndw->func->image_clr(wndw); 116 117 return flush ? wndw->func->update(wndw, interlock) : 0; 118 } 119 120 u32 121 nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 interlock, 122 struct nv50_wndw_atom *asyw) 123 { 124 if (interlock) { 125 asyw->image.mode = 0; 126 asyw->image.interval = 1; 127 } 128 129 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw); 130 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw); 131 if (asyw->set.image) wndw->func->image_set(wndw, asyw); 132 if (asyw->set.lut ) wndw->func->lut (wndw, asyw); 133 if (asyw->set.point) { 134 wndw->immd->point(wndw, asyw); 135 wndw->immd->update(wndw, interlock); 136 } 137 138 return wndw->func->update ? wndw->func->update(wndw, interlock) : 0; 139 } 140 141 static void 142 nv50_wndw_atomic_check_release(struct nv50_wndw *wndw, 143 struct nv50_wndw_atom *asyw, 144 struct nv50_head_atom *asyh) 145 { 146 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev); 147 NV_ATOMIC(drm, "%s release\n", wndw->plane.name); 148 wndw->func->release(wndw, asyw, asyh); 149 asyw->ntfy.handle = 0; 150 asyw->sema.handle = 0; 151 } 152 153 static int 154 nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw, 155 struct nv50_wndw_atom *asyw, 156 struct nv50_head_atom *asyh) 157 { 158 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb); 159 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev); 160 int ret; 161 162 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name); 163 164 asyw->image.w = fb->base.width; 165 asyw->image.h = fb->base.height; 166 asyw->image.kind = fb->nvbo->kind; 167 168 if (asyh->state.pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) 169 asyw->interval = 0; 170 else 171 asyw->interval = 1; 172 173 if (asyw->image.kind) { 174 asyw->image.layout = 0; 175 if (drm->client.device.info.chipset >= 0xc0) 176 asyw->image.block = fb->nvbo->mode >> 4; 177 else 178 asyw->image.block = fb->nvbo->mode; 179 asyw->image.pitch = (fb->base.pitches[0] / 4) << 4; 180 } else { 181 asyw->image.layout = 1; 182 asyw->image.block = 0; 183 asyw->image.pitch = fb->base.pitches[0]; 184 } 185 186 ret = wndw->func->acquire(wndw, asyw, asyh); 187 if (ret) 188 return ret; 189 190 if (asyw->set.image) { 191 if (!(asyw->image.mode = asyw->interval ? 0 : 1)) 192 asyw->image.interval = asyw->interval; 193 else 194 asyw->image.interval = 0; 195 } 196 197 return 0; 198 } 199 200 int 201 nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state) 202 { 203 struct nouveau_drm *drm = nouveau_drm(plane->dev); 204 struct nv50_wndw *wndw = nv50_wndw(plane); 205 struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state); 206 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state); 207 struct nv50_head_atom *harm = NULL, *asyh = NULL; 208 bool varm = false, asyv = false, asym = false; 209 int ret; 210 211 NV_ATOMIC(drm, "%s atomic_check\n", plane->name); 212 if (asyw->state.crtc) { 213 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc); 214 if (IS_ERR(asyh)) 215 return PTR_ERR(asyh); 216 asym = drm_atomic_crtc_needs_modeset(&asyh->state); 217 asyv = asyh->state.active; 218 } 219 220 if (armw->state.crtc) { 221 harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc); 222 if (IS_ERR(harm)) 223 return PTR_ERR(harm); 224 varm = harm->state.crtc->state->active; 225 } 226 227 if (asyv) { 228 asyw->point.x = asyw->state.crtc_x; 229 asyw->point.y = asyw->state.crtc_y; 230 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point))) 231 asyw->set.point = true; 232 233 ret = nv50_wndw_atomic_check_acquire(wndw, asyw, asyh); 234 if (ret) 235 return ret; 236 } else 237 if (varm) { 238 nv50_wndw_atomic_check_release(wndw, asyw, harm); 239 } else { 240 return 0; 241 } 242 243 if (!asyv || asym) { 244 asyw->clr.ntfy = armw->ntfy.handle != 0; 245 asyw->clr.sema = armw->sema.handle != 0; 246 if (wndw->func->image_clr) 247 asyw->clr.image = armw->image.handle != 0; 248 asyw->set.lut = wndw->func->lut && asyv; 249 } 250 251 return 0; 252 } 253 254 static void 255 nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state) 256 { 257 struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb); 258 struct nouveau_drm *drm = nouveau_drm(plane->dev); 259 260 NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb); 261 if (!old_state->fb) 262 return; 263 264 nouveau_bo_unpin(fb->nvbo); 265 } 266 267 static int 268 nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state) 269 { 270 struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb); 271 struct nouveau_drm *drm = nouveau_drm(plane->dev); 272 struct nv50_wndw *wndw = nv50_wndw(plane); 273 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state); 274 struct nv50_head_atom *asyh; 275 struct nv50_wndw_ctxdma *ctxdma; 276 int ret; 277 278 NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb); 279 if (!asyw->state.fb) 280 return 0; 281 282 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true); 283 if (ret) 284 return ret; 285 286 ctxdma = nv50_wndw_ctxdma_new(wndw, fb); 287 if (IS_ERR(ctxdma)) { 288 nouveau_bo_unpin(fb->nvbo); 289 return PTR_ERR(ctxdma); 290 } 291 292 asyw->state.fence = reservation_object_get_excl_rcu(fb->nvbo->bo.resv); 293 asyw->image.handle = ctxdma->object.handle; 294 asyw->image.offset = fb->nvbo->bo.offset; 295 296 if (wndw->func->prepare) { 297 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc); 298 if (IS_ERR(asyh)) 299 return PTR_ERR(asyh); 300 301 wndw->func->prepare(wndw, asyh, asyw); 302 } 303 304 return 0; 305 } 306 307 static const struct drm_plane_helper_funcs 308 nv50_wndw_helper = { 309 .prepare_fb = nv50_wndw_prepare_fb, 310 .cleanup_fb = nv50_wndw_cleanup_fb, 311 .atomic_check = nv50_wndw_atomic_check, 312 }; 313 314 static void 315 nv50_wndw_atomic_destroy_state(struct drm_plane *plane, 316 struct drm_plane_state *state) 317 { 318 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state); 319 __drm_atomic_helper_plane_destroy_state(&asyw->state); 320 kfree(asyw); 321 } 322 323 static struct drm_plane_state * 324 nv50_wndw_atomic_duplicate_state(struct drm_plane *plane) 325 { 326 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state); 327 struct nv50_wndw_atom *asyw; 328 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL))) 329 return NULL; 330 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state); 331 asyw->interval = 1; 332 asyw->sema = armw->sema; 333 asyw->ntfy = armw->ntfy; 334 asyw->image = armw->image; 335 asyw->point = armw->point; 336 asyw->lut = armw->lut; 337 asyw->clr.mask = 0; 338 asyw->set.mask = 0; 339 return &asyw->state; 340 } 341 342 static void 343 nv50_wndw_reset(struct drm_plane *plane) 344 { 345 struct nv50_wndw_atom *asyw; 346 347 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL)))) 348 return; 349 350 if (plane->state) 351 plane->funcs->atomic_destroy_state(plane, plane->state); 352 plane->state = &asyw->state; 353 plane->state->plane = plane; 354 plane->state->rotation = DRM_MODE_ROTATE_0; 355 } 356 357 static void 358 nv50_wndw_destroy(struct drm_plane *plane) 359 { 360 struct nv50_wndw *wndw = nv50_wndw(plane); 361 struct nv50_wndw_ctxdma *ctxdma, *ctxtmp; 362 363 list_for_each_entry_safe(ctxdma, ctxtmp, &wndw->ctxdma.list, head) { 364 nv50_wndw_ctxdma_del(ctxdma); 365 } 366 367 nvif_notify_fini(&wndw->notify); 368 nv50_dmac_destroy(&wndw->wimm); 369 nv50_dmac_destroy(&wndw->wndw); 370 drm_plane_cleanup(&wndw->plane); 371 kfree(wndw); 372 } 373 374 const struct drm_plane_funcs 375 nv50_wndw = { 376 .update_plane = drm_atomic_helper_update_plane, 377 .disable_plane = drm_atomic_helper_disable_plane, 378 .destroy = nv50_wndw_destroy, 379 .reset = nv50_wndw_reset, 380 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state, 381 .atomic_destroy_state = nv50_wndw_atomic_destroy_state, 382 }; 383 384 static int 385 nv50_wndw_notify(struct nvif_notify *notify) 386 { 387 return NVIF_NOTIFY_KEEP; 388 } 389 390 void 391 nv50_wndw_fini(struct nv50_wndw *wndw) 392 { 393 nvif_notify_put(&wndw->notify); 394 } 395 396 void 397 nv50_wndw_init(struct nv50_wndw *wndw) 398 { 399 nvif_notify_get(&wndw->notify); 400 } 401 402 int 403 nv50_wndw_new_(const struct nv50_wndw_func *func, struct drm_device *dev, 404 enum drm_plane_type type, const char *name, int index, 405 const u32 *format, struct nv50_wndw **pwndw) 406 { 407 struct nv50_wndw *wndw; 408 int nformat; 409 int ret; 410 411 if (!(wndw = *pwndw = kzalloc(sizeof(*wndw), GFP_KERNEL))) 412 return -ENOMEM; 413 wndw->func = func; 414 wndw->id = index; 415 416 wndw->ctxdma.parent = &wndw->wndw.base.user; 417 INIT_LIST_HEAD(&wndw->ctxdma.list); 418 419 for (nformat = 0; format[nformat]; nformat++); 420 421 ret = drm_universal_plane_init(dev, &wndw->plane, 0, &nv50_wndw, 422 format, nformat, NULL, 423 type, "%s-%d", name, index); 424 if (ret) { 425 kfree(*pwndw); 426 *pwndw = NULL; 427 return ret; 428 } 429 430 drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper); 431 432 wndw->notify.func = nv50_wndw_notify; 433 return 0; 434 } 435