]> git.ipfire.org Git - thirdparty/linux.git/blob - drivers/gpu/drm/nouveau/dispnv50/wndw.c
drm/nouveau/kms/nv50-: store window visibility in state
[thirdparty/linux.git] / drivers / gpu / drm / nouveau / dispnv50 / wndw.c
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 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
102 if (asyw->set.ntfy) {
103 return wndw->func->ntfy_wait_begun(disp->sync,
104 asyw->ntfy.offset,
105 wndw->wndw.base.device);
106 }
107 return 0;
108 }
109
110 void
111 nv50_wndw_flush_clr(struct nv50_wndw *wndw, u32 *interlock, bool flush,
112 struct nv50_wndw_atom *asyw)
113 {
114 union nv50_wndw_atom_mask clr = {
115 .mask = asyw->clr.mask & ~(flush ? 0 : asyw->set.mask),
116 };
117 if (clr.sema ) wndw->func-> sema_clr(wndw);
118 if (clr.ntfy ) wndw->func-> ntfy_clr(wndw);
119 if (clr.image) wndw->func->image_clr(wndw);
120
121 interlock[wndw->interlock.type] |= wndw->interlock.data;
122 }
123
124 void
125 nv50_wndw_flush_set(struct nv50_wndw *wndw, u32 *interlock,
126 struct nv50_wndw_atom *asyw)
127 {
128 if (interlock) {
129 asyw->image.mode = 0;
130 asyw->image.interval = 1;
131 }
132
133 if (asyw->set.sema ) wndw->func->sema_set (wndw, asyw);
134 if (asyw->set.ntfy ) wndw->func->ntfy_set (wndw, asyw);
135 if (asyw->set.image) wndw->func->image_set(wndw, asyw);
136 if (asyw->set.lut ) wndw->func->lut (wndw, asyw);
137 if (asyw->set.point) {
138 wndw->immd->point(wndw, asyw);
139 wndw->immd->update(wndw, interlock);
140 }
141
142 interlock[wndw->interlock.type] |= wndw->interlock.data;
143 }
144
145 void
146 nv50_wndw_ntfy_enable(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
147 {
148 struct nv50_disp *disp = nv50_disp(wndw->plane.dev);
149
150 asyw->ntfy.handle = wndw->wndw.sync.handle;
151 asyw->ntfy.offset = wndw->ntfy;
152 asyw->ntfy.awaken = false;
153 asyw->set.ntfy = true;
154
155 wndw->func->ntfy_reset(disp->sync, wndw->ntfy);
156 wndw->ntfy ^= 0x10;
157 }
158
159 static void
160 nv50_wndw_atomic_check_release(struct nv50_wndw *wndw,
161 struct nv50_wndw_atom *asyw,
162 struct nv50_head_atom *asyh)
163 {
164 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
165 NV_ATOMIC(drm, "%s release\n", wndw->plane.name);
166 wndw->func->release(wndw, asyw, asyh);
167 asyw->ntfy.handle = 0;
168 asyw->sema.handle = 0;
169 }
170
171 static int
172 nv50_wndw_atomic_check_acquire_rgb(struct nv50_wndw_atom *asyw)
173 {
174 switch (asyw->state.fb->format->format) {
175 case DRM_FORMAT_C8 : asyw->image.format = 0x1e; break;
176 case DRM_FORMAT_XRGB8888 :
177 case DRM_FORMAT_ARGB8888 : asyw->image.format = 0xcf; break;
178 case DRM_FORMAT_RGB565 : asyw->image.format = 0xe8; break;
179 case DRM_FORMAT_XRGB1555 :
180 case DRM_FORMAT_ARGB1555 : asyw->image.format = 0xe9; break;
181 case DRM_FORMAT_XBGR2101010:
182 case DRM_FORMAT_ABGR2101010: asyw->image.format = 0xd1; break;
183 case DRM_FORMAT_XBGR8888 :
184 case DRM_FORMAT_ABGR8888 : asyw->image.format = 0xd5; break;
185 default:
186 WARN_ON(1);
187 return -EINVAL;
188 }
189 return 0;
190 }
191
192 static int
193 nv50_wndw_atomic_check_acquire(struct nv50_wndw *wndw,
194 struct nv50_wndw_atom *asyw,
195 struct nv50_head_atom *asyh)
196 {
197 struct nouveau_framebuffer *fb = nouveau_framebuffer(asyw->state.fb);
198 struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
199 int ret;
200
201 NV_ATOMIC(drm, "%s acquire\n", wndw->plane.name);
202
203 asyw->image.w = fb->base.width;
204 asyw->image.h = fb->base.height;
205 asyw->image.kind = fb->nvbo->kind;
206
207 ret = nv50_wndw_atomic_check_acquire_rgb(asyw);
208 if (ret)
209 return ret;
210
211 if (asyw->image.kind) {
212 asyw->image.layout = 0;
213 if (drm->client.device.info.chipset >= 0xc0)
214 asyw->image.block = fb->nvbo->mode >> 4;
215 else
216 asyw->image.block = fb->nvbo->mode;
217 asyw->image.pitch[0] = (fb->base.pitches[0] / 4) << 4;
218 } else {
219 asyw->image.layout = 1;
220 asyw->image.block = 0;
221 asyw->image.pitch[0] = fb->base.pitches[0];
222 }
223
224 ret = wndw->func->acquire(wndw, asyw, asyh);
225 if (ret)
226 return ret;
227
228 if (asyw->set.image) {
229 if (!(asyh->state.pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC))
230 asyw->image.interval = 1;
231 else
232 asyw->image.interval = 0;
233 asyw->image.mode = asyw->image.interval ? 0 : 1;
234 }
235
236 return 0;
237 }
238
239 int
240 nv50_wndw_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
241 {
242 struct nouveau_drm *drm = nouveau_drm(plane->dev);
243 struct nv50_wndw *wndw = nv50_wndw(plane);
244 struct nv50_wndw_atom *armw = nv50_wndw_atom(wndw->plane.state);
245 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
246 struct nv50_head_atom *harm = NULL, *asyh = NULL;
247 bool modeset = false;
248 int ret;
249
250 NV_ATOMIC(drm, "%s atomic_check\n", plane->name);
251
252 /* Fetch the assembly state for the head the window will belong to,
253 * and determine whether the window will be visible.
254 */
255 if (asyw->state.crtc) {
256 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
257 if (IS_ERR(asyh))
258 return PTR_ERR(asyh);
259 modeset = drm_atomic_crtc_needs_modeset(&asyh->state);
260 asyw->visible = asyh->state.active;
261 } else {
262 asyw->visible = false;
263 }
264
265 /* Fetch assembly state for the head the window used to belong to. */
266 if (armw->state.crtc) {
267 harm = nv50_head_atom_get(asyw->state.state, armw->state.crtc);
268 if (IS_ERR(harm))
269 return PTR_ERR(harm);
270 }
271
272 /* Calculate new window state. */
273 if (asyw->visible) {
274 asyw->point.x = asyw->state.crtc_x;
275 asyw->point.y = asyw->state.crtc_y;
276 if (memcmp(&armw->point, &asyw->point, sizeof(asyw->point)))
277 asyw->set.point = true;
278
279 ret = nv50_wndw_atomic_check_acquire(wndw, asyw, asyh);
280 if (ret)
281 return ret;
282 } else
283 if (armw->visible) {
284 nv50_wndw_atomic_check_release(wndw, asyw, harm);
285 } else {
286 return 0;
287 }
288
289 /* Aside from the obvious case where the window is actively being
290 * disabled, we might also need to temporarily disable the window
291 * when performing certain modeset operations.
292 */
293 if (!asyw->visible || modeset) {
294 asyw->clr.ntfy = armw->ntfy.handle != 0;
295 asyw->clr.sema = armw->sema.handle != 0;
296 if (wndw->func->image_clr)
297 asyw->clr.image = armw->image.handle[0] != 0;
298 asyw->set.lut = wndw->func->lut && asyw->visible;
299 }
300
301 return 0;
302 }
303
304 static void
305 nv50_wndw_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
306 {
307 struct nouveau_framebuffer *fb = nouveau_framebuffer(old_state->fb);
308 struct nouveau_drm *drm = nouveau_drm(plane->dev);
309
310 NV_ATOMIC(drm, "%s cleanup: %p\n", plane->name, old_state->fb);
311 if (!old_state->fb)
312 return;
313
314 nouveau_bo_unpin(fb->nvbo);
315 }
316
317 static int
318 nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
319 {
320 struct nouveau_framebuffer *fb = nouveau_framebuffer(state->fb);
321 struct nouveau_drm *drm = nouveau_drm(plane->dev);
322 struct nv50_wndw *wndw = nv50_wndw(plane);
323 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
324 struct nv50_head_atom *asyh;
325 struct nv50_wndw_ctxdma *ctxdma;
326 int ret;
327
328 NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
329 if (!asyw->state.fb)
330 return 0;
331
332 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
333 if (ret)
334 return ret;
335
336 ctxdma = nv50_wndw_ctxdma_new(wndw, fb);
337 if (IS_ERR(ctxdma)) {
338 nouveau_bo_unpin(fb->nvbo);
339 return PTR_ERR(ctxdma);
340 }
341
342 asyw->state.fence = reservation_object_get_excl_rcu(fb->nvbo->bo.resv);
343 asyw->image.handle[0] = ctxdma->object.handle;
344 asyw->image.offset[0] = fb->nvbo->bo.offset;
345
346 if (wndw->func->prepare) {
347 asyh = nv50_head_atom_get(asyw->state.state, asyw->state.crtc);
348 if (IS_ERR(asyh))
349 return PTR_ERR(asyh);
350
351 wndw->func->prepare(wndw, asyh, asyw);
352 }
353
354 return 0;
355 }
356
357 static const struct drm_plane_helper_funcs
358 nv50_wndw_helper = {
359 .prepare_fb = nv50_wndw_prepare_fb,
360 .cleanup_fb = nv50_wndw_cleanup_fb,
361 .atomic_check = nv50_wndw_atomic_check,
362 };
363
364 static void
365 nv50_wndw_atomic_destroy_state(struct drm_plane *plane,
366 struct drm_plane_state *state)
367 {
368 struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
369 __drm_atomic_helper_plane_destroy_state(&asyw->state);
370 kfree(asyw);
371 }
372
373 static struct drm_plane_state *
374 nv50_wndw_atomic_duplicate_state(struct drm_plane *plane)
375 {
376 struct nv50_wndw_atom *armw = nv50_wndw_atom(plane->state);
377 struct nv50_wndw_atom *asyw;
378 if (!(asyw = kmalloc(sizeof(*asyw), GFP_KERNEL)))
379 return NULL;
380 __drm_atomic_helper_plane_duplicate_state(plane, &asyw->state);
381 asyw->sema = armw->sema;
382 asyw->ntfy = armw->ntfy;
383 asyw->image = armw->image;
384 asyw->point = armw->point;
385 asyw->lut = armw->lut;
386 asyw->clr.mask = 0;
387 asyw->set.mask = 0;
388 return &asyw->state;
389 }
390
391 static void
392 nv50_wndw_reset(struct drm_plane *plane)
393 {
394 struct nv50_wndw_atom *asyw;
395
396 if (WARN_ON(!(asyw = kzalloc(sizeof(*asyw), GFP_KERNEL))))
397 return;
398
399 if (plane->state)
400 plane->funcs->atomic_destroy_state(plane, plane->state);
401 plane->state = &asyw->state;
402 plane->state->plane = plane;
403 plane->state->rotation = DRM_MODE_ROTATE_0;
404 }
405
406 static void
407 nv50_wndw_destroy(struct drm_plane *plane)
408 {
409 struct nv50_wndw *wndw = nv50_wndw(plane);
410 struct nv50_wndw_ctxdma *ctxdma, *ctxtmp;
411
412 list_for_each_entry_safe(ctxdma, ctxtmp, &wndw->ctxdma.list, head) {
413 nv50_wndw_ctxdma_del(ctxdma);
414 }
415
416 nvif_notify_fini(&wndw->notify);
417 nv50_dmac_destroy(&wndw->wimm);
418 nv50_dmac_destroy(&wndw->wndw);
419 drm_plane_cleanup(&wndw->plane);
420 kfree(wndw);
421 }
422
423 const struct drm_plane_funcs
424 nv50_wndw = {
425 .update_plane = drm_atomic_helper_update_plane,
426 .disable_plane = drm_atomic_helper_disable_plane,
427 .destroy = nv50_wndw_destroy,
428 .reset = nv50_wndw_reset,
429 .atomic_duplicate_state = nv50_wndw_atomic_duplicate_state,
430 .atomic_destroy_state = nv50_wndw_atomic_destroy_state,
431 };
432
433 static int
434 nv50_wndw_notify(struct nvif_notify *notify)
435 {
436 return NVIF_NOTIFY_KEEP;
437 }
438
439 void
440 nv50_wndw_fini(struct nv50_wndw *wndw)
441 {
442 nvif_notify_put(&wndw->notify);
443 }
444
445 void
446 nv50_wndw_init(struct nv50_wndw *wndw)
447 {
448 nvif_notify_get(&wndw->notify);
449 }
450
451 int
452 nv50_wndw_new_(const struct nv50_wndw_func *func, struct drm_device *dev,
453 enum drm_plane_type type, const char *name, int index,
454 const u32 *format, u32 heads,
455 enum nv50_disp_interlock_type interlock_type, u32 interlock_data,
456 struct nv50_wndw **pwndw)
457 {
458 struct nv50_wndw *wndw;
459 int nformat;
460 int ret;
461
462 if (!(wndw = *pwndw = kzalloc(sizeof(*wndw), GFP_KERNEL)))
463 return -ENOMEM;
464 wndw->func = func;
465 wndw->id = index;
466 wndw->interlock.type = interlock_type;
467 wndw->interlock.data = interlock_data;
468 wndw->ctxdma.parent = &wndw->wndw.base.user;
469
470 wndw->ctxdma.parent = &wndw->wndw.base.user;
471 INIT_LIST_HEAD(&wndw->ctxdma.list);
472
473 for (nformat = 0; format[nformat]; nformat++);
474
475 ret = drm_universal_plane_init(dev, &wndw->plane, heads, &nv50_wndw,
476 format, nformat, NULL,
477 type, "%s-%d", name, index);
478 if (ret) {
479 kfree(*pwndw);
480 *pwndw = NULL;
481 return ret;
482 }
483
484 drm_plane_helper_add(&wndw->plane, &nv50_wndw_helper);
485
486 wndw->notify.func = nv50_wndw_notify;
487 return 0;
488 }