1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright (C) 2009-2010 Francisco Jerez.
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All Rights Reserved.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * a copy of this software and associated documentation files (the
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "Software"), to deal in the Software without restriction, including
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribute, sublicense, and/or sell copies of the Software, and to
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the following conditions:
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * next paragraph) shall be included in all copies or substantial
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * portions of the Software.
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "nouveau_driver.h"
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "nouveau_context.h"
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Returns a pointer to a chunk of 'size' bytes long GART memory. 'bo'
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and 'offset' will point to the returned memory.
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid *
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnouveau_get_scratch(struct gl_context *ctx, unsigned size,
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    struct nouveau_bo **bo, unsigned *offset)
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	struct nouveau_client *client = context_client(ctx);
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	struct nouveau_scratch_state *scratch =
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		&to_nouveau_context(ctx)->scratch;
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	void *buf;
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	if (scratch->buf && size <= NOUVEAU_SCRATCH_SIZE - scratch->offset) {
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		nouveau_bo_ref(scratch->bo[scratch->index], bo);
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		buf = scratch->buf + scratch->offset;
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		*offset = scratch->offset;
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		scratch->offset += size;
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	} else if (size <= NOUVEAU_SCRATCH_SIZE) {
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		scratch->index = (scratch->index + 1) % NOUVEAU_SCRATCH_COUNT;
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		nouveau_bo_ref(scratch->bo[scratch->index], bo);
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		nouveau_bo_map(*bo, NOUVEAU_BO_WR, client);
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		buf = scratch->buf = (*bo)->map;
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		*offset = 0;
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		scratch->offset = size;
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	} else {
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_GART |
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			       NOUVEAU_BO_MAP, 0, size, NULL, bo);
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		nouveau_bo_map(*bo, NOUVEAU_BO_WR, client);
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		buf = (*bo)->map;
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		*offset = 0;
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	}
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	return buf;
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnouveau_scratch_init(struct gl_context *ctx)
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	struct nouveau_scratch_state *scratch =
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		&to_nouveau_context(ctx)->scratch;
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	int ret, i;
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	for (i = 0; i < NOUVEAU_SCRATCH_COUNT; i++) {
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		ret = nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_GART |
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				     NOUVEAU_BO_MAP, 0, NOUVEAU_SCRATCH_SIZE,
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				     NULL, &scratch->bo[i]);
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		assert(!ret);
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	}
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnouveau_scratch_destroy(struct gl_context *ctx)
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	struct nouveau_scratch_state *scratch =
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		&to_nouveau_context(ctx)->scratch;
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	int i;
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	for (i = 0; i < NOUVEAU_SCRATCH_COUNT; i++)
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		nouveau_bo_ref(NULL, &scratch->bo[i]);
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
98