summaryrefslogtreecommitdiff
path: root/radeon_device.c
blob: 22490f364389b686618e49428106eb508e554f0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright © 2010 Jerome Glisse <glisse@freedesktop.org>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "radeon_device.h"

#pragma pack(1)
struct ib_reloc_gem {
    uint32_t    handle;
    uint32_t    read_domain;
    uint32_t    write_domain;
    uint32_t    flags;
};
#pragma pack()
#define RELOC_SIZE (sizeof(struct ib_reloc_gem) / sizeof(uint32_t))

u32 radeon_ib_reloc(struct radeon_ib *ib, struct radeon_bo *bo, u32 d)
{
	struct ib_reloc_gem *reloc;
	int i;

	for (i = 0; i < ib->crelocs; i++) {
		reloc = (struct ib_reloc_gem*)&ib->relocs[i * RELOC_SIZE];
		if (reloc->handle == bo->handle) {
			reloc->read_domain |= d;
			return (i * RELOC_SIZE);
		}
	}
	if (ib->crelocs >= ib->nrelocs)
		return 0xFFFFFFFFUL;
	i = ib->crelocs++;
	reloc = (struct ib_reloc_gem*)&ib->relocs[i * RELOC_SIZE];
	reloc->handle = bo->handle;
	reloc->read_domain = d;
	reloc->write_domain = 0;
	reloc->flags = 0;
	return (i * RELOC_SIZE);
}

int radeon_ib_get(struct radeon_device *rdev, struct radeon_ib **ib)
{
	struct radeon_ib *lib;

	lib = malloc(sizeof(struct radeon_ib));
	if (lib == NULL)
		return -ENOMEM;
	memset(lib, sizeof(*lib), 0);
	lib->ptr = malloc(64 * 1024);
	if (lib->ptr == NULL) {
		free(lib);
		return -ENOMEM;
	}
	lib->cpkts = 0;
	lib->length_dw = 64 * 1024 / 4;
	lib->relocs = malloc(64 * 1024);
	if (lib->relocs == NULL) {
		free(lib->ptr);
		free(lib);
		return -ENOMEM;
	}
	lib->nrelocs = 64 * 1024 / RELOC_SIZE;
	lib->crelocs = 0;
	*ib = lib;
	return 0;
}

void radeon_ib_free(struct radeon_ib *ib)
{
	if (ib == NULL)
		return;
	free(ib->ptr);
	free(ib->relocs);
	free(ib);
}

int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
{
	struct drm_radeon_cs drmib;
	struct drm_radeon_cs_chunk chunks[2];
	uint64_t chunk_array[2];
	int r = 0;

#if 0
	for (r = 0; r < ib->cpkts; r++) {
		printf("0x%08X\n", ib->ptr[r]);
	}
#endif
	drmib.num_chunks = 2;
	drmib.chunks = (uint64_t)(uintptr_t)chunk_array;
	chunks[0].chunk_id = RADEON_CHUNK_ID_IB;
	chunks[0].length_dw = ib->cpkts;
	chunks[0].chunk_data = (uint64_t)(uintptr_t)ib->ptr;
	chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS;
	chunks[1].length_dw = ib->crelocs * 4;
	chunks[1].chunk_data = (uint64_t)(uintptr_t)ib->relocs;
	chunk_array[0] = (uint64_t)(uintptr_t)&chunks[0];
	chunk_array[1] = (uint64_t)(uintptr_t)&chunks[1];
#if 1
	r = drmCommandWriteRead(rdev->fd, DRM_RADEON_CS, &drmib,
				sizeof(struct drm_radeon_cs));
#endif
	return r;
}

int radeon_device_init(struct radeon_device **rdev, struct radeon *radeon)
{
	struct radeon_device *dev;
	int r;

	*rdev = NULL;
	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
	if (dev == NULL)
		return -ENOMEM;
	memset(dev, 0, sizeof(struct radeon_device));
	dev->radeon = radeon;
	dev->fd = radeon->fd;
	r = r600_atoms_init(dev);
	if (r)
		return r;
	*rdev = dev;
	return r;
}

void radeon_device_release(struct radeon_device *rdev)
{
	r600_atoms_release(rdev);
	memset(rdev, 0, sizeof(struct radeon_device));
	kfree(rdev);
}

void kref_set(struct kref *kref, int num)
{
	kref->refcount = num;
}

/**
 * kref_init - initialize object.
 * @kref: object in question.
 */
void kref_init(struct kref *kref)
{
	kref_set(kref, 1);
}

/**
 * kref_get - increment refcount for object.
 * @kref: object.
 */
void kref_get(struct kref *kref)
{
	kref->refcount++;
}

/**
 * kref_put - decrement refcount for object.
 * @kref: object.
 * @release: pointer to the function that will clean up the object when the
 *	     last reference to the object is released.
 *	     This pointer is required, and it is not acceptable to pass kfree
 *	     in as this function.
 *
 * Decrement the refcount, and if 0, call release().
 * Return 1 if the object was removed, otherwise return 0.  Beware, if this
 * function returns 0, you still can not count on the kref from remaining in
 * memory.  Only use the return value if you want to see if the kref is now
 * gone, not present.
 */
int kref_put(struct kref *kref, void (*release)(struct kref *kref))
{
	kref->refcount--;
	if (!kref->refcount) {
		release(kref);
		return 1;
	}
	return 0;
}