Skip to content

Commit 627664d

Browse files
Ben Skeggsairlied
authored andcommitted
drm/nouveau: add helper functions for allocating pinned/cpu-mapped bos
Replace some awkward sequences that are repeated in a number of places with helper functions. Signed-off-by: Ben Skeggs <[email protected]> Reviewed-by: Dave Airlie <[email protected]> Reviewed-by: Timur Tabi <[email protected]> Tested-by: Timur Tabi <[email protected]> Signed-off-by: Dave Airlie <[email protected]>
1 parent 44f93b2 commit 627664d

File tree

10 files changed

+81
-108
lines changed

10 files changed

+81
-108
lines changed

drivers/gpu/drm/nouveau/dispnv04/crtc.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,7 @@ static void nv_crtc_destroy(struct drm_crtc *crtc)
768768
disp->image[nv_crtc->index] = NULL;
769769
}
770770

771-
nouveau_bo_unmap(nv_crtc->cursor.nvbo);
772-
nouveau_bo_unpin(nv_crtc->cursor.nvbo);
773-
nouveau_bo_fini(nv_crtc->cursor.nvbo);
771+
nouveau_bo_unpin_del(&nv_crtc->cursor.nvbo);
774772
nvif_event_dtor(&nv_crtc->vblank);
775773
nvif_head_dtor(&nv_crtc->head);
776774
kfree(nv_crtc);
@@ -1303,6 +1301,7 @@ nv04_crtc_vblank_handler(struct nvif_event *event, void *repv, u32 repc)
13031301
int
13041302
nv04_crtc_create(struct drm_device *dev, int crtc_num)
13051303
{
1304+
struct nouveau_cli *cli = &nouveau_drm(dev)->client;
13061305
struct nouveau_display *disp = nouveau_display(dev);
13071306
struct nouveau_crtc *nv_crtc;
13081307
struct drm_plane *primary;
@@ -1336,20 +1335,9 @@ nv04_crtc_create(struct drm_device *dev, int crtc_num)
13361335
drm_crtc_helper_add(&nv_crtc->base, &nv04_crtc_helper_funcs);
13371336
drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
13381337

1339-
ret = nouveau_bo_new(&nouveau_drm(dev)->client, 64*64*4, 0x100,
1340-
NOUVEAU_GEM_DOMAIN_VRAM, 0, 0x0000, NULL, NULL,
1341-
&nv_crtc->cursor.nvbo);
1342-
if (!ret) {
1343-
ret = nouveau_bo_pin(nv_crtc->cursor.nvbo,
1344-
NOUVEAU_GEM_DOMAIN_VRAM, false);
1345-
if (!ret) {
1346-
ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
1347-
if (ret)
1348-
nouveau_bo_unpin(nv_crtc->cursor.nvbo);
1349-
}
1350-
if (ret)
1351-
nouveau_bo_fini(nv_crtc->cursor.nvbo);
1352-
}
1338+
ret = nouveau_bo_new_map(cli, NOUVEAU_GEM_DOMAIN_VRAM, 64 * 64 * 4, &nv_crtc->cursor.nvbo);
1339+
if (ret)
1340+
return ret;
13531341

13541342
nv04_cursor_init(nv_crtc);
13551343

drivers/gpu/drm/nouveau/dispnv50/disp.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,10 +2808,7 @@ nv50_display_destroy(struct drm_device *dev)
28082808
nvif_object_dtor(&disp->caps);
28092809
nv50_core_del(&disp->core);
28102810

2811-
nouveau_bo_unmap(disp->sync);
2812-
if (disp->sync)
2813-
nouveau_bo_unpin(disp->sync);
2814-
nouveau_bo_fini(disp->sync);
2811+
nouveau_bo_unpin_del(&disp->sync);
28152812

28162813
nouveau_display(dev)->priv = NULL;
28172814
kfree(disp);
@@ -2843,20 +2840,7 @@ nv50_display_create(struct drm_device *dev)
28432840
dev->mode_config.normalize_zpos = true;
28442841

28452842
/* small shared memory area we use for notifiers and semaphores */
2846-
ret = nouveau_bo_new(&drm->client, 4096, 0x1000,
2847-
NOUVEAU_GEM_DOMAIN_VRAM,
2848-
0, 0x0000, NULL, NULL, &disp->sync);
2849-
if (!ret) {
2850-
ret = nouveau_bo_pin(disp->sync, NOUVEAU_GEM_DOMAIN_VRAM, true);
2851-
if (!ret) {
2852-
ret = nouveau_bo_map(disp->sync);
2853-
if (ret)
2854-
nouveau_bo_unpin(disp->sync);
2855-
}
2856-
if (ret)
2857-
nouveau_bo_fini(disp->sync);
2858-
}
2859-
2843+
ret = nouveau_bo_new_map(&drm->client, NOUVEAU_GEM_DOMAIN_VRAM, PAGE_SIZE, &disp->sync);
28602844
if (ret)
28612845
goto out;
28622846

drivers/gpu/drm/nouveau/nouveau_bo.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,61 @@ nouveau_bo_new(struct nouveau_cli *cli, u64 size, int align,
401401
return 0;
402402
}
403403

404+
void
405+
nouveau_bo_unpin_del(struct nouveau_bo **pnvbo)
406+
{
407+
struct nouveau_bo *nvbo = *pnvbo;
408+
409+
if (!nvbo)
410+
return;
411+
412+
nouveau_bo_unmap(nvbo);
413+
nouveau_bo_unpin(nvbo);
414+
nouveau_bo_fini(nvbo);
415+
416+
*pnvbo = NULL;
417+
}
418+
419+
int
420+
nouveau_bo_new_pin(struct nouveau_cli *cli, u32 domain, u32 size, struct nouveau_bo **pnvbo)
421+
{
422+
struct nouveau_bo *nvbo;
423+
int ret;
424+
425+
ret = nouveau_bo_new(cli, size, 0, domain, 0, 0, NULL, NULL, &nvbo);
426+
if (ret)
427+
return ret;
428+
429+
ret = nouveau_bo_pin(nvbo, domain, false);
430+
if (ret) {
431+
nouveau_bo_fini(nvbo);
432+
return ret;
433+
}
434+
435+
*pnvbo = nvbo;
436+
return 0;
437+
}
438+
439+
int
440+
nouveau_bo_new_map(struct nouveau_cli *cli, u32 domain, u32 size, struct nouveau_bo **pnvbo)
441+
{
442+
struct nouveau_bo *nvbo;
443+
int ret;
444+
445+
ret = nouveau_bo_new_pin(cli, domain, size, &nvbo);
446+
if (ret)
447+
return ret;
448+
449+
ret = nouveau_bo_map(nvbo);
450+
if (ret) {
451+
nouveau_bo_unpin_del(&nvbo);
452+
return ret;
453+
}
454+
455+
*pnvbo = nvbo;
456+
return 0;
457+
}
458+
404459
static void
405460
set_placement_range(struct nouveau_bo *nvbo, uint32_t domain)
406461
{

drivers/gpu/drm/nouveau/nouveau_bo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct nouveau_channel;
99
struct nouveau_cli;
1010
struct nouveau_drm;
1111
struct nouveau_fence;
12+
struct nouveau_vma;
1213

1314
struct nouveau_bo {
1415
struct ttm_buffer_object bo;
@@ -89,6 +90,10 @@ void nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo);
8990
void nouveau_bo_add_io_reserve_lru(struct ttm_buffer_object *bo);
9091
void nouveau_bo_del_io_reserve_lru(struct ttm_buffer_object *bo);
9192

93+
int nouveau_bo_new_pin(struct nouveau_cli *, u32 domain, u32 size, struct nouveau_bo **);
94+
int nouveau_bo_new_map(struct nouveau_cli *, u32 domain, u32 size, struct nouveau_bo **);
95+
void nouveau_bo_unpin_del(struct nouveau_bo **);
96+
9297
/* TODO: submit equivalent to TTM generic API upstream? */
9398
static inline void __iomem *
9499
nvbo_kmap_obj_iovirtual(struct nouveau_bo *nvbo)

drivers/gpu/drm/nouveau/nouveau_chan.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ nouveau_channel_del(struct nouveau_channel **pchan)
105105
nvif_mem_dtor(&chan->mem_userd);
106106
nvif_object_dtor(&chan->push.ctxdma);
107107
nouveau_vma_del(&chan->push.vma);
108-
nouveau_bo_unmap(chan->push.buffer);
109-
if (chan->push.buffer && chan->push.buffer->bo.pin_count)
110-
nouveau_bo_unpin(chan->push.buffer);
111-
nouveau_bo_fini(chan->push.buffer);
108+
nouveau_bo_unpin_del(&chan->push.buffer);
112109
kfree(chan);
113110
}
114111
*pchan = NULL;
@@ -163,14 +160,7 @@ nouveau_channel_prep(struct nouveau_cli *cli,
163160
if (nouveau_vram_pushbuf)
164161
target = NOUVEAU_GEM_DOMAIN_VRAM;
165162

166-
ret = nouveau_bo_new(cli, size, 0, target, 0, 0, NULL, NULL,
167-
&chan->push.buffer);
168-
if (ret == 0) {
169-
ret = nouveau_bo_pin(chan->push.buffer, target, false);
170-
if (ret == 0)
171-
ret = nouveau_bo_map(chan->push.buffer);
172-
}
173-
163+
ret = nouveau_bo_new_map(cli, target, size, &chan->push.buffer);
174164
if (ret) {
175165
nouveau_channel_del(pchan);
176166
return ret;

drivers/gpu/drm/nouveau/nouveau_dmem.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,15 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage)
256256
chunk->pagemap.ops = &nouveau_dmem_pagemap_ops;
257257
chunk->pagemap.owner = drm->dev;
258258

259-
ret = nouveau_bo_new(&drm->client, DMEM_CHUNK_SIZE, 0,
260-
NOUVEAU_GEM_DOMAIN_VRAM, 0, 0, NULL, NULL,
261-
&chunk->bo);
259+
ret = nouveau_bo_new_pin(&drm->client, NOUVEAU_GEM_DOMAIN_VRAM, DMEM_CHUNK_SIZE,
260+
&chunk->bo);
262261
if (ret)
263262
goto out_release;
264263

265-
ret = nouveau_bo_pin(chunk->bo, NOUVEAU_GEM_DOMAIN_VRAM, false);
266-
if (ret)
267-
goto out_bo_free;
268-
269264
ptr = memremap_pages(&chunk->pagemap, numa_node_id());
270265
if (IS_ERR(ptr)) {
271266
ret = PTR_ERR(ptr);
272-
goto out_bo_unpin;
267+
goto out_bo_free;
273268
}
274269

275270
mutex_lock(&drm->dmem->mutex);
@@ -292,10 +287,8 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage)
292287

293288
return 0;
294289

295-
out_bo_unpin:
296-
nouveau_bo_unpin(chunk->bo);
297290
out_bo_free:
298-
nouveau_bo_fini(chunk->bo);
291+
nouveau_bo_unpin_del(&chunk->bo);
299292
out_release:
300293
release_mem_region(chunk->pagemap.range.start, range_len(&chunk->pagemap.range));
301294
out_free:
@@ -426,8 +419,7 @@ nouveau_dmem_fini(struct nouveau_drm *drm)
426419

427420
list_for_each_entry_safe(chunk, tmp, &drm->dmem->chunks, list) {
428421
nouveau_dmem_evict_chunk(chunk);
429-
nouveau_bo_unpin(chunk->bo);
430-
nouveau_bo_fini(chunk->bo);
422+
nouveau_bo_unpin_del(&chunk->bo);
431423
WARN_ON(chunk->callocated);
432424
list_del(&chunk->list);
433425
memunmap_pages(&chunk->pagemap);

drivers/gpu/drm/nouveau/nv10_fence.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,8 @@ void
8585
nv10_fence_destroy(struct nouveau_drm *drm)
8686
{
8787
struct nv10_fence_priv *priv = drm->fence;
88-
nouveau_bo_unmap(priv->bo);
89-
if (priv->bo)
90-
nouveau_bo_unpin(priv->bo);
91-
nouveau_bo_fini(priv->bo);
88+
89+
nouveau_bo_unpin_del(&priv->bo);
9290
drm->fence = NULL;
9391
kfree(priv);
9492
}

drivers/gpu/drm/nouveau/nv17_fence.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,7 @@ nv17_fence_create(struct nouveau_drm *drm)
130130
priv->base.context_del = nv10_fence_context_del;
131131
spin_lock_init(&priv->lock);
132132

133-
ret = nouveau_bo_new(&drm->client, 4096, 0x1000,
134-
NOUVEAU_GEM_DOMAIN_VRAM,
135-
0, 0x0000, NULL, NULL, &priv->bo);
136-
if (!ret) {
137-
ret = nouveau_bo_pin(priv->bo, NOUVEAU_GEM_DOMAIN_VRAM, false);
138-
if (!ret) {
139-
ret = nouveau_bo_map(priv->bo);
140-
if (ret)
141-
nouveau_bo_unpin(priv->bo);
142-
}
143-
if (ret)
144-
nouveau_bo_fini(priv->bo);
145-
}
146-
133+
ret = nouveau_bo_new_map(&drm->client, NOUVEAU_GEM_DOMAIN_VRAM, PAGE_SIZE, &priv->bo);
147134
if (ret) {
148135
nv10_fence_destroy(drm);
149136
return ret;

drivers/gpu/drm/nouveau/nv50_fence.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,7 @@ nv50_fence_create(struct nouveau_drm *drm)
8181
priv->base.context_del = nv10_fence_context_del;
8282
spin_lock_init(&priv->lock);
8383

84-
ret = nouveau_bo_new(&drm->client, 4096, 0x1000,
85-
NOUVEAU_GEM_DOMAIN_VRAM,
86-
0, 0x0000, NULL, NULL, &priv->bo);
87-
if (!ret) {
88-
ret = nouveau_bo_pin(priv->bo, NOUVEAU_GEM_DOMAIN_VRAM, false);
89-
if (!ret) {
90-
ret = nouveau_bo_map(priv->bo);
91-
if (ret)
92-
nouveau_bo_unpin(priv->bo);
93-
}
94-
if (ret)
95-
nouveau_bo_fini(priv->bo);
96-
}
97-
84+
ret = nouveau_bo_new_map(&drm->client, NOUVEAU_GEM_DOMAIN_VRAM, PAGE_SIZE, &priv->bo);
9885
if (ret) {
9986
nv10_fence_destroy(drm);
10087
return ret;

drivers/gpu/drm/nouveau/nv84_fence.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,8 @@ static void
185185
nv84_fence_destroy(struct nouveau_drm *drm)
186186
{
187187
struct nv84_fence_priv *priv = drm->fence;
188-
nouveau_bo_unmap(priv->bo);
189-
if (priv->bo)
190-
nouveau_bo_unpin(priv->bo);
191-
nouveau_bo_fini(priv->bo);
188+
189+
nouveau_bo_unpin_del(&priv->bo);
192190
drm->fence = NULL;
193191
kfree(priv);
194192
}
@@ -222,19 +220,8 @@ nv84_fence_create(struct nouveau_drm *drm)
222220
* will lose CPU/GPU coherency!
223221
*/
224222
NOUVEAU_GEM_DOMAIN_GART | NOUVEAU_GEM_DOMAIN_COHERENT;
225-
ret = nouveau_bo_new(&drm->client, 16 * drm->chan_total, 0,
226-
domain, 0, 0, NULL, NULL, &priv->bo);
227-
if (ret == 0) {
228-
ret = nouveau_bo_pin(priv->bo, domain, false);
229-
if (ret == 0) {
230-
ret = nouveau_bo_map(priv->bo);
231-
if (ret)
232-
nouveau_bo_unpin(priv->bo);
233-
}
234-
if (ret)
235-
nouveau_bo_fini(priv->bo);
236-
}
237223

224+
ret = nouveau_bo_new_map(&drm->client, domain, 16 * drm->chan_total, &priv->bo);
238225
if (ret)
239226
nv84_fence_destroy(drm);
240227
return ret;

0 commit comments

Comments
 (0)