Subversion Repositories Kolibri OS

Rev

Rev 5271 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5271 Rev 6104
1
/*
1
/*
2
 * Copyright 2011 Advanced Micro Devices, Inc.
2
 * Copyright 2011 Advanced Micro Devices, Inc.
3
 * All Rights Reserved.
3
 * All Rights Reserved.
4
 *
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the
6
 * copy of this software and associated documentation files (the
7
 * "Software"), to deal in the Software without restriction, including
7
 * "Software"), to deal in the Software without restriction, including
8
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * without limitation the rights to use, copy, modify, merge, publish,
9
 * distribute, sub license, and/or sell copies of the Software, and to
9
 * distribute, sub license, and/or sell copies of the Software, and to
10
 * permit persons to whom the Software is furnished to do so, subject to
10
 * permit persons to whom the Software is furnished to do so, subject to
11
 * the following conditions:
11
 * the following conditions:
12
 *
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
19
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20
 *
20
 *
21
 * The above copyright notice and this permission notice (including the
21
 * The above copyright notice and this permission notice (including the
22
 * next paragraph) shall be included in all copies or substantial portions
22
 * next paragraph) shall be included in all copies or substantial portions
23
 * of the Software.
23
 * of the Software.
24
 *
24
 *
25
 */
25
 */
26
/*
26
/*
27
 * Authors:
27
 * Authors:
28
 *    Christian König 
28
 *    Christian König 
29
 */
29
 */
30
 
30
 
31
#include 
31
#include 
32
#include 
32
#include 
33
#include 
33
#include 
34
#include 
34
#include 
35
 
35
 
36
#include "radeon.h"
36
#include "radeon.h"
37
#include "r600d.h"
37
#include "r600d.h"
38
 
38
 
39
/* 1 second timeout */
39
/* 1 second timeout */
40
#define UVD_IDLE_TIMEOUT_MS	1000
40
#define UVD_IDLE_TIMEOUT_MS	1000
41
 
41
 
42
/* Firmware Names */
42
/* Firmware Names */
-
 
43
#define FIRMWARE_R600		"radeon/R600_uvd.bin"
-
 
44
#define FIRMWARE_RS780		"radeon/RS780_uvd.bin"
-
 
45
#define FIRMWARE_RV770		"radeon/RV770_uvd.bin"
43
#define FIRMWARE_RV710		"radeon/RV710_uvd.bin"
46
#define FIRMWARE_RV710		"radeon/RV710_uvd.bin"
44
#define FIRMWARE_CYPRESS	"radeon/CYPRESS_uvd.bin"
47
#define FIRMWARE_CYPRESS	"radeon/CYPRESS_uvd.bin"
45
#define FIRMWARE_SUMO		"radeon/SUMO_uvd.bin"
48
#define FIRMWARE_SUMO		"radeon/SUMO_uvd.bin"
46
#define FIRMWARE_TAHITI		"radeon/TAHITI_uvd.bin"
49
#define FIRMWARE_TAHITI		"radeon/TAHITI_uvd.bin"
47
#define FIRMWARE_BONAIRE	"radeon/BONAIRE_uvd.bin"
50
#define FIRMWARE_BONAIRE	"radeon/BONAIRE_uvd.bin"
48
 
51
 
49
MODULE_FIRMWARE(FIRMWARE_R600);
52
MODULE_FIRMWARE(FIRMWARE_R600);
50
MODULE_FIRMWARE(FIRMWARE_RS780);
53
MODULE_FIRMWARE(FIRMWARE_RS780);
51
MODULE_FIRMWARE(FIRMWARE_RV770);
54
MODULE_FIRMWARE(FIRMWARE_RV770);
52
MODULE_FIRMWARE(FIRMWARE_RV710);
55
MODULE_FIRMWARE(FIRMWARE_RV710);
53
MODULE_FIRMWARE(FIRMWARE_CYPRESS);
56
MODULE_FIRMWARE(FIRMWARE_CYPRESS);
54
MODULE_FIRMWARE(FIRMWARE_SUMO);
57
MODULE_FIRMWARE(FIRMWARE_SUMO);
55
MODULE_FIRMWARE(FIRMWARE_TAHITI);
58
MODULE_FIRMWARE(FIRMWARE_TAHITI);
56
MODULE_FIRMWARE(FIRMWARE_BONAIRE);
59
MODULE_FIRMWARE(FIRMWARE_BONAIRE);
57
 
60
 
58
static void radeon_uvd_idle_work_handler(struct work_struct *work);
61
static void radeon_uvd_idle_work_handler(struct work_struct *work);
59
 
62
 
60
int radeon_uvd_init(struct radeon_device *rdev)
63
int radeon_uvd_init(struct radeon_device *rdev)
61
{
64
{
62
	unsigned long bo_size;
65
	unsigned long bo_size;
63
	const char *fw_name;
66
	const char *fw_name;
64
	int i, r;
67
	int i, r;
65
 
68
 
66
//   INIT_DELAYED_WORK(&rdev->uvd.idle_work, radeon_uvd_idle_work_handler);
69
//   INIT_DELAYED_WORK(&rdev->uvd.idle_work, radeon_uvd_idle_work_handler);
67
 
70
 
68
	switch (rdev->family) {
71
	switch (rdev->family) {
-
 
72
	case CHIP_RV610:
-
 
73
	case CHIP_RV630:
-
 
74
	case CHIP_RV670:
-
 
75
	case CHIP_RV620:
-
 
76
	case CHIP_RV635:
-
 
77
		fw_name = FIRMWARE_R600;
-
 
78
		break;
-
 
79
 
-
 
80
	case CHIP_RS780:
-
 
81
	case CHIP_RS880:
-
 
82
		fw_name = FIRMWARE_RS780;
-
 
83
		break;
-
 
84
 
-
 
85
	case CHIP_RV770:
-
 
86
		fw_name = FIRMWARE_RV770;
-
 
87
		break;
-
 
88
 
69
	case CHIP_RV710:
89
	case CHIP_RV710:
70
	case CHIP_RV730:
90
	case CHIP_RV730:
71
	case CHIP_RV740:
91
	case CHIP_RV740:
72
		fw_name = FIRMWARE_RV710;
92
		fw_name = FIRMWARE_RV710;
73
		break;
93
		break;
74
 
94
 
75
	case CHIP_CYPRESS:
95
	case CHIP_CYPRESS:
76
	case CHIP_HEMLOCK:
96
	case CHIP_HEMLOCK:
77
	case CHIP_JUNIPER:
97
	case CHIP_JUNIPER:
78
	case CHIP_REDWOOD:
98
	case CHIP_REDWOOD:
79
	case CHIP_CEDAR:
99
	case CHIP_CEDAR:
80
		fw_name = FIRMWARE_CYPRESS;
100
		fw_name = FIRMWARE_CYPRESS;
81
		break;
101
		break;
82
 
102
 
83
	case CHIP_SUMO:
103
	case CHIP_SUMO:
84
	case CHIP_SUMO2:
104
	case CHIP_SUMO2:
85
	case CHIP_PALM:
105
	case CHIP_PALM:
86
	case CHIP_CAYMAN:
106
	case CHIP_CAYMAN:
87
	case CHIP_BARTS:
107
	case CHIP_BARTS:
88
	case CHIP_TURKS:
108
	case CHIP_TURKS:
89
	case CHIP_CAICOS:
109
	case CHIP_CAICOS:
90
		fw_name = FIRMWARE_SUMO;
110
		fw_name = FIRMWARE_SUMO;
91
		break;
111
		break;
92
 
112
 
93
	case CHIP_TAHITI:
113
	case CHIP_TAHITI:
94
	case CHIP_VERDE:
114
	case CHIP_VERDE:
95
	case CHIP_PITCAIRN:
115
	case CHIP_PITCAIRN:
96
	case CHIP_ARUBA:
116
	case CHIP_ARUBA:
97
	case CHIP_OLAND:
117
	case CHIP_OLAND:
98
		fw_name = FIRMWARE_TAHITI;
118
		fw_name = FIRMWARE_TAHITI;
99
		break;
119
		break;
100
 
120
 
101
	case CHIP_BONAIRE:
121
	case CHIP_BONAIRE:
102
	case CHIP_KABINI:
122
	case CHIP_KABINI:
103
	case CHIP_KAVERI:
123
	case CHIP_KAVERI:
104
	case CHIP_HAWAII:
124
	case CHIP_HAWAII:
105
	case CHIP_MULLINS:
125
	case CHIP_MULLINS:
106
		fw_name = FIRMWARE_BONAIRE;
126
		fw_name = FIRMWARE_BONAIRE;
107
		break;
127
		break;
108
 
128
 
109
	default:
129
	default:
110
		return -EINVAL;
130
		return -EINVAL;
111
	}
131
	}
112
 
132
 
113
	r = request_firmware(&rdev->uvd_fw, fw_name, rdev->dev);
133
	r = request_firmware(&rdev->uvd_fw, fw_name, rdev->dev);
114
	if (r) {
134
	if (r) {
115
		dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
135
		dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
116
			fw_name);
136
			fw_name);
117
		return r;
137
		return r;
118
	}
138
	}
119
 
139
 
120
	bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
140
	bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
121
		  RADEON_UVD_STACK_SIZE + RADEON_UVD_HEAP_SIZE +
141
		  RADEON_UVD_STACK_SIZE + RADEON_UVD_HEAP_SIZE +
122
		  RADEON_GPU_PAGE_SIZE;
142
		  RADEON_GPU_PAGE_SIZE;
123
	r = radeon_bo_create(rdev, bo_size, PAGE_SIZE, true,
143
	r = radeon_bo_create(rdev, bo_size, PAGE_SIZE, true,
124
			     RADEON_GEM_DOMAIN_VRAM, 0, NULL,
144
			     RADEON_GEM_DOMAIN_VRAM, 0, NULL,
125
			     NULL, &rdev->uvd.vcpu_bo);
145
			     NULL, &rdev->uvd.vcpu_bo);
126
	if (r) {
146
	if (r) {
127
		dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
147
		dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
128
		return r;
148
		return r;
129
	}
149
	}
130
 
150
 
131
	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
151
	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
132
	if (r) {
152
	if (r) {
133
		radeon_bo_unref(&rdev->uvd.vcpu_bo);
153
		radeon_bo_unref(&rdev->uvd.vcpu_bo);
134
		dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
154
		dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
135
		return r;
155
		return r;
136
	}
156
	}
137
 
157
 
138
	r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
158
	r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
139
			  &rdev->uvd.gpu_addr);
159
			  &rdev->uvd.gpu_addr);
140
	if (r) {
160
	if (r) {
141
		radeon_bo_unreserve(rdev->uvd.vcpu_bo);
161
		radeon_bo_unreserve(rdev->uvd.vcpu_bo);
142
		radeon_bo_unref(&rdev->uvd.vcpu_bo);
162
		radeon_bo_unref(&rdev->uvd.vcpu_bo);
143
		dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
163
		dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
144
		return r;
164
		return r;
145
	}
165
	}
146
 
166
 
147
	r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
167
	r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
148
	if (r) {
168
	if (r) {
149
		dev_err(rdev->dev, "(%d) UVD map failed\n", r);
169
		dev_err(rdev->dev, "(%d) UVD map failed\n", r);
150
		return r;
170
		return r;
151
	}
171
	}
152
 
172
 
153
	radeon_bo_unreserve(rdev->uvd.vcpu_bo);
173
	radeon_bo_unreserve(rdev->uvd.vcpu_bo);
154
 
174
 
155
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
175
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
156
		atomic_set(&rdev->uvd.handles[i], 0);
176
		atomic_set(&rdev->uvd.handles[i], 0);
157
		rdev->uvd.filp[i] = NULL;
177
		rdev->uvd.filp[i] = NULL;
158
		rdev->uvd.img_size[i] = 0;
178
		rdev->uvd.img_size[i] = 0;
159
	}
179
	}
160
 
180
 
161
	return 0;
181
	return 0;
162
}
182
}
163
 
183
 
164
void radeon_uvd_fini(struct radeon_device *rdev)
184
void radeon_uvd_fini(struct radeon_device *rdev)
165
{
185
{
166
	int r;
186
	int r;
167
 
187
 
168
	if (rdev->uvd.vcpu_bo == NULL)
188
	if (rdev->uvd.vcpu_bo == NULL)
169
		return;
189
		return;
170
 
190
 
171
	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
191
	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
172
	if (!r) {
192
	if (!r) {
173
		radeon_bo_kunmap(rdev->uvd.vcpu_bo);
193
		radeon_bo_kunmap(rdev->uvd.vcpu_bo);
174
		radeon_bo_unpin(rdev->uvd.vcpu_bo);
194
		radeon_bo_unpin(rdev->uvd.vcpu_bo);
175
		radeon_bo_unreserve(rdev->uvd.vcpu_bo);
195
		radeon_bo_unreserve(rdev->uvd.vcpu_bo);
176
	}
196
	}
177
 
197
 
178
	radeon_bo_unref(&rdev->uvd.vcpu_bo);
198
	radeon_bo_unref(&rdev->uvd.vcpu_bo);
179
 
199
 
180
	radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_UVD_INDEX]);
200
	radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_UVD_INDEX]);
181
 
201
 
182
	release_firmware(rdev->uvd_fw);
202
	release_firmware(rdev->uvd_fw);
183
}
203
}
184
 
204
 
185
int radeon_uvd_suspend(struct radeon_device *rdev)
205
int radeon_uvd_suspend(struct radeon_device *rdev)
186
{
206
{
187
	unsigned size;
-
 
188
	void *ptr;
-
 
189
	int i;
207
	int i, r;
190
 
208
 
191
	if (rdev->uvd.vcpu_bo == NULL)
209
	if (rdev->uvd.vcpu_bo == NULL)
192
		return 0;
210
		return 0;
193
 
211
 
194
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
212
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
195
		if (atomic_read(&rdev->uvd.handles[i]))
213
		uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
-
 
214
		if (handle != 0) {
196
			break;
215
			struct radeon_fence *fence;
197
 
-
 
198
	if (i == RADEON_MAX_UVD_HANDLES)
216
 
-
 
217
			radeon_uvd_note_usage(rdev);
-
 
218
 
199
		return 0;
219
			r = radeon_uvd_get_destroy_msg(rdev,
-
 
220
				R600_RING_TYPE_UVD_INDEX, handle, &fence);
-
 
221
			if (r) {
200
 
222
				DRM_ERROR("Error destroying UVD (%d)!\n", r);
201
	size = radeon_bo_size(rdev->uvd.vcpu_bo);
223
				continue;
202
	size -= rdev->uvd_fw->size;
224
			}
203
 
225
 
-
 
226
			radeon_fence_wait(fence, false);
-
 
227
			radeon_fence_unref(&fence);
204
	ptr = rdev->uvd.cpu_addr;
228
 
205
	ptr += rdev->uvd_fw->size;
229
			rdev->uvd.filp[i] = NULL;
206
 
230
			atomic_set(&rdev->uvd.handles[i], 0);
207
	rdev->uvd.saved_bo = kmalloc(size, GFP_KERNEL);
231
		}
208
	memcpy(rdev->uvd.saved_bo, ptr, size);
232
	}
209
 
233
 
210
	return 0;
234
	return 0;
211
}
235
}
212
 
236
 
213
int radeon_uvd_resume(struct radeon_device *rdev)
237
int radeon_uvd_resume(struct radeon_device *rdev)
214
{
238
{
215
	unsigned size;
239
	unsigned size;
216
	void *ptr;
240
	void *ptr;
217
 
241
 
218
	if (rdev->uvd.vcpu_bo == NULL)
242
	if (rdev->uvd.vcpu_bo == NULL)
219
		return -EINVAL;
243
		return -EINVAL;
220
 
244
 
221
	memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
245
	memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
222
 
246
 
223
	size = radeon_bo_size(rdev->uvd.vcpu_bo);
247
	size = radeon_bo_size(rdev->uvd.vcpu_bo);
224
	size -= rdev->uvd_fw->size;
248
	size -= rdev->uvd_fw->size;
225
 
249
 
226
	ptr = rdev->uvd.cpu_addr;
250
	ptr = rdev->uvd.cpu_addr;
227
	ptr += rdev->uvd_fw->size;
251
	ptr += rdev->uvd_fw->size;
228
 
-
 
229
	if (rdev->uvd.saved_bo != NULL) {
-
 
230
		memcpy(ptr, rdev->uvd.saved_bo, size);
-
 
231
		kfree(rdev->uvd.saved_bo);
-
 
232
		rdev->uvd.saved_bo = NULL;
-
 
233
	} else
252
 
234
		memset(ptr, 0, size);
253
	memset(ptr, 0, size);
235
 
254
 
236
	return 0;
255
	return 0;
237
}
256
}
238
 
257
 
239
void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo,
258
void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo,
240
				       uint32_t allowed_domains)
259
				       uint32_t allowed_domains)
241
{
260
{
242
	int i;
261
	int i;
243
 
262
 
244
	for (i = 0; i < rbo->placement.num_placement; ++i) {
263
	for (i = 0; i < rbo->placement.num_placement; ++i) {
245
		rbo->placements[i].fpfn = 0 >> PAGE_SHIFT;
264
		rbo->placements[i].fpfn = 0 >> PAGE_SHIFT;
246
		rbo->placements[i].lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
265
		rbo->placements[i].lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
247
	}
266
	}
248
 
267
 
249
	/* If it must be in VRAM it must be in the first segment as well */
268
	/* If it must be in VRAM it must be in the first segment as well */
250
	if (allowed_domains == RADEON_GEM_DOMAIN_VRAM)
269
	if (allowed_domains == RADEON_GEM_DOMAIN_VRAM)
251
		return;
270
		return;
252
 
271
 
253
	/* abort if we already have more than one placement */
272
	/* abort if we already have more than one placement */
254
	if (rbo->placement.num_placement > 1)
273
	if (rbo->placement.num_placement > 1)
255
		return;
274
		return;
256
 
275
 
257
	/* add another 256MB segment */
276
	/* add another 256MB segment */
258
	rbo->placements[1] = rbo->placements[0];
277
	rbo->placements[1] = rbo->placements[0];
259
	rbo->placements[1].fpfn += (256 * 1024 * 1024) >> PAGE_SHIFT;
278
	rbo->placements[1].fpfn += (256 * 1024 * 1024) >> PAGE_SHIFT;
260
	rbo->placements[1].lpfn += (256 * 1024 * 1024) >> PAGE_SHIFT;
279
	rbo->placements[1].lpfn += (256 * 1024 * 1024) >> PAGE_SHIFT;
261
	rbo->placement.num_placement++;
280
	rbo->placement.num_placement++;
262
	rbo->placement.num_busy_placement++;
281
	rbo->placement.num_busy_placement++;
263
}
282
}
264
 
283
 
265
void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
284
void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
266
{
285
{
267
	int i, r;
286
	int i, r;
268
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
287
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
269
		uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
288
		uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
270
		if (handle != 0 && rdev->uvd.filp[i] == filp) {
289
		if (handle != 0 && rdev->uvd.filp[i] == filp) {
271
			struct radeon_fence *fence;
290
			struct radeon_fence *fence;
272
 
291
 
273
			radeon_uvd_note_usage(rdev);
292
			radeon_uvd_note_usage(rdev);
274
 
293
 
275
			r = radeon_uvd_get_destroy_msg(rdev,
294
			r = radeon_uvd_get_destroy_msg(rdev,
276
				R600_RING_TYPE_UVD_INDEX, handle, &fence);
295
				R600_RING_TYPE_UVD_INDEX, handle, &fence);
277
			if (r) {
296
			if (r) {
278
				DRM_ERROR("Error destroying UVD (%d)!\n", r);
297
				DRM_ERROR("Error destroying UVD (%d)!\n", r);
279
				continue;
298
				continue;
280
			}
299
			}
281
 
300
 
282
			radeon_fence_wait(fence, false);
301
			radeon_fence_wait(fence, false);
283
			radeon_fence_unref(&fence);
302
			radeon_fence_unref(&fence);
284
 
303
 
285
			rdev->uvd.filp[i] = NULL;
304
			rdev->uvd.filp[i] = NULL;
286
			atomic_set(&rdev->uvd.handles[i], 0);
305
			atomic_set(&rdev->uvd.handles[i], 0);
287
		}
306
		}
288
	}
307
	}
289
}
308
}
290
 
309
 
291
static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
310
static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
292
{
311
{
293
	unsigned stream_type = msg[4];
312
	unsigned stream_type = msg[4];
294
	unsigned width = msg[6];
313
	unsigned width = msg[6];
295
	unsigned height = msg[7];
314
	unsigned height = msg[7];
296
	unsigned dpb_size = msg[9];
315
	unsigned dpb_size = msg[9];
297
	unsigned pitch = msg[28];
316
	unsigned pitch = msg[28];
298
 
317
 
299
	unsigned width_in_mb = width / 16;
318
	unsigned width_in_mb = width / 16;
300
	unsigned height_in_mb = ALIGN(height / 16, 2);
319
	unsigned height_in_mb = ALIGN(height / 16, 2);
301
 
320
 
302
	unsigned image_size, tmp, min_dpb_size;
321
	unsigned image_size, tmp, min_dpb_size;
303
 
322
 
304
	image_size = width * height;
323
	image_size = width * height;
305
	image_size += image_size / 2;
324
	image_size += image_size / 2;
306
	image_size = ALIGN(image_size, 1024);
325
	image_size = ALIGN(image_size, 1024);
307
 
326
 
308
	switch (stream_type) {
327
	switch (stream_type) {
309
	case 0: /* H264 */
328
	case 0: /* H264 */
310
 
329
 
311
		/* reference picture buffer */
330
		/* reference picture buffer */
312
		min_dpb_size = image_size * 17;
331
		min_dpb_size = image_size * 17;
313
 
332
 
314
		/* macroblock context buffer */
333
		/* macroblock context buffer */
315
		min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
334
		min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
316
 
335
 
317
		/* IT surface buffer */
336
		/* IT surface buffer */
318
		min_dpb_size += width_in_mb * height_in_mb * 32;
337
		min_dpb_size += width_in_mb * height_in_mb * 32;
319
		break;
338
		break;
320
 
339
 
321
	case 1: /* VC1 */
340
	case 1: /* VC1 */
322
 
341
 
323
		/* reference picture buffer */
342
		/* reference picture buffer */
324
		min_dpb_size = image_size * 3;
343
		min_dpb_size = image_size * 3;
325
 
344
 
326
		/* CONTEXT_BUFFER */
345
		/* CONTEXT_BUFFER */
327
		min_dpb_size += width_in_mb * height_in_mb * 128;
346
		min_dpb_size += width_in_mb * height_in_mb * 128;
328
 
347
 
329
		/* IT surface buffer */
348
		/* IT surface buffer */
330
		min_dpb_size += width_in_mb * 64;
349
		min_dpb_size += width_in_mb * 64;
331
 
350
 
332
		/* DB surface buffer */
351
		/* DB surface buffer */
333
		min_dpb_size += width_in_mb * 128;
352
		min_dpb_size += width_in_mb * 128;
334
 
353
 
335
		/* BP */
354
		/* BP */
336
		tmp = max(width_in_mb, height_in_mb);
355
		tmp = max(width_in_mb, height_in_mb);
337
		min_dpb_size += ALIGN(tmp * 7 * 16, 64);
356
		min_dpb_size += ALIGN(tmp * 7 * 16, 64);
338
		break;
357
		break;
339
 
358
 
340
	case 3: /* MPEG2 */
359
	case 3: /* MPEG2 */
341
 
360
 
342
		/* reference picture buffer */
361
		/* reference picture buffer */
343
		min_dpb_size = image_size * 3;
362
		min_dpb_size = image_size * 3;
344
		break;
363
		break;
345
 
364
 
346
	case 4: /* MPEG4 */
365
	case 4: /* MPEG4 */
347
 
366
 
348
		/* reference picture buffer */
367
		/* reference picture buffer */
349
		min_dpb_size = image_size * 3;
368
		min_dpb_size = image_size * 3;
350
 
369
 
351
		/* CM */
370
		/* CM */
352
		min_dpb_size += width_in_mb * height_in_mb * 64;
371
		min_dpb_size += width_in_mb * height_in_mb * 64;
353
 
372
 
354
		/* IT surface buffer */
373
		/* IT surface buffer */
355
		min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
374
		min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
356
		break;
375
		break;
357
 
376
 
358
	default:
377
	default:
359
		DRM_ERROR("UVD codec not handled %d!\n", stream_type);
378
		DRM_ERROR("UVD codec not handled %d!\n", stream_type);
360
		return -EINVAL;
379
		return -EINVAL;
361
	}
380
	}
362
 
381
 
363
	if (width > pitch) {
382
	if (width > pitch) {
364
		DRM_ERROR("Invalid UVD decoding target pitch!\n");
383
		DRM_ERROR("Invalid UVD decoding target pitch!\n");
365
		return -EINVAL;
384
		return -EINVAL;
366
	}
385
	}
367
 
386
 
368
	if (dpb_size < min_dpb_size) {
387
	if (dpb_size < min_dpb_size) {
369
		DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
388
		DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
370
			  dpb_size, min_dpb_size);
389
			  dpb_size, min_dpb_size);
371
		return -EINVAL;
390
		return -EINVAL;
372
	}
391
	}
373
 
392
 
374
	buf_sizes[0x1] = dpb_size;
393
	buf_sizes[0x1] = dpb_size;
375
	buf_sizes[0x2] = image_size;
394
	buf_sizes[0x2] = image_size;
376
	return 0;
395
	return 0;
377
}
396
}
-
 
397
 
-
 
398
static int radeon_uvd_validate_codec(struct radeon_cs_parser *p,
-
 
399
				     unsigned stream_type)
-
 
400
{
-
 
401
	switch (stream_type) {
-
 
402
	case 0: /* H264 */
-
 
403
	case 1: /* VC1 */
-
 
404
		/* always supported */
-
 
405
		return 0;
-
 
406
 
-
 
407
	case 3: /* MPEG2 */
-
 
408
	case 4: /* MPEG4 */
-
 
409
		/* only since UVD 3 */
-
 
410
		if (p->rdev->family >= CHIP_PALM)
-
 
411
			return 0;
-
 
412
 
-
 
413
		/* fall through */
-
 
414
	default:
-
 
415
		DRM_ERROR("UVD codec not supported by hardware %d!\n",
-
 
416
			  stream_type);
-
 
417
		return -EINVAL;
-
 
418
	}
-
 
419
}
378
 
420
 
379
static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
421
static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
380
			     unsigned offset, unsigned buf_sizes[])
422
			     unsigned offset, unsigned buf_sizes[])
381
{
423
{
382
	int32_t *msg, msg_type, handle;
424
	int32_t *msg, msg_type, handle;
383
	unsigned img_size = 0;
425
	unsigned img_size = 0;
384
	struct fence *f;
426
	struct fence *f;
385
	void *ptr;
427
	void *ptr;
386
 
428
 
387
	int i, r;
429
	int i, r;
388
 
430
 
389
	if (offset & 0x3F) {
431
	if (offset & 0x3F) {
390
		DRM_ERROR("UVD messages must be 64 byte aligned!\n");
432
		DRM_ERROR("UVD messages must be 64 byte aligned!\n");
391
		return -EINVAL;
433
		return -EINVAL;
392
	}
434
	}
393
 
435
 
394
	f = reservation_object_get_excl(bo->tbo.resv);
436
	f = reservation_object_get_excl(bo->tbo.resv);
395
	if (f) {
437
	if (f) {
396
		r = radeon_fence_wait((struct radeon_fence *)f, false);
438
		r = radeon_fence_wait((struct radeon_fence *)f, false);
397
		if (r) {
439
		if (r) {
398
			DRM_ERROR("Failed waiting for UVD message (%d)!\n", r);
440
			DRM_ERROR("Failed waiting for UVD message (%d)!\n", r);
399
			return r;
441
			return r;
400
		}
442
		}
401
	}
443
	}
402
 
444
 
403
	r = radeon_bo_kmap(bo, &ptr);
445
	r = radeon_bo_kmap(bo, &ptr);
404
	if (r) {
446
	if (r) {
405
		DRM_ERROR("Failed mapping the UVD message (%d)!\n", r);
447
		DRM_ERROR("Failed mapping the UVD message (%d)!\n", r);
406
		return r;
448
		return r;
407
	}
449
	}
408
 
450
 
409
	msg = ptr + offset;
451
	msg = ptr + offset;
410
 
452
 
411
	msg_type = msg[1];
453
	msg_type = msg[1];
412
	handle = msg[2];
454
	handle = msg[2];
413
 
455
 
414
	if (handle == 0) {
456
	if (handle == 0) {
415
		DRM_ERROR("Invalid UVD handle!\n");
457
		DRM_ERROR("Invalid UVD handle!\n");
416
		return -EINVAL;
458
		return -EINVAL;
417
	}
459
	}
418
 
460
 
419
	if (msg_type == 1) {
-
 
420
		/* it's a decode msg, calc buffer sizes */
-
 
421
		r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
-
 
422
		/* calc image size (width * height) */
-
 
423
		img_size = msg[6] * msg[7];
-
 
424
		radeon_bo_kunmap(bo);
-
 
425
		if (r)
-
 
426
			return r;
-
 
427
 
-
 
428
	} else if (msg_type == 2) {
-
 
429
		/* it's a destroy msg, free the handle */
-
 
430
		for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
-
 
431
			atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
-
 
432
		radeon_bo_kunmap(bo);
-
 
433
		return 0;
461
	switch (msg_type) {
434
	} else {
462
	case 0:
435
		/* it's a create msg, calc image size (width * height) */
463
		/* it's a create msg, calc image size (width * height) */
-
 
464
		img_size = msg[7] * msg[8];
-
 
465
 
436
		img_size = msg[7] * msg[8];
466
		r = radeon_uvd_validate_codec(p, msg[4]);
-
 
467
		radeon_bo_kunmap(bo);
-
 
468
		if (r)
437
		radeon_bo_kunmap(bo);
469
			return r;
-
 
470
 
-
 
471
		/* try to alloc a new handle */
438
 
472
		for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
439
		if (msg_type != 0) {
473
			if (atomic_read(&p->rdev->uvd.handles[i]) == handle) {
440
			DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
474
				DRM_ERROR("Handle 0x%x already in use!\n", handle);
441
			return -EINVAL;
475
				return -EINVAL;
442
		}
476
			}
443
 
477
 
-
 
478
			if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
-
 
479
				p->rdev->uvd.filp[i] = p->filp;
-
 
480
				p->rdev->uvd.img_size[i] = img_size;
-
 
481
				return 0;
444
		/* it's a create msg, no special handling needed */
482
			}
-
 
483
		}
-
 
484
 
-
 
485
		DRM_ERROR("No more free UVD handles!\n");
-
 
486
		return -EINVAL;
-
 
487
 
-
 
488
	case 1:
-
 
489
		/* it's a decode msg, validate codec and calc buffer sizes */
-
 
490
		r = radeon_uvd_validate_codec(p, msg[4]);
-
 
491
		if (!r)
-
 
492
			r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
-
 
493
		radeon_bo_kunmap(bo);
-
 
494
		if (r)
445
	}
495
			return r;
446
 
496
 
447
	/* create or decode, validate the handle */
497
		/* validate the handle */
-
 
498
		for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
-
 
499
			if (atomic_read(&p->rdev->uvd.handles[i]) == handle) {
-
 
500
				if (p->rdev->uvd.filp[i] != p->filp) {
-
 
501
					DRM_ERROR("UVD handle collision detected!\n");
448
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
502
					return -EINVAL;
449
		if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
503
				}
-
 
504
				return 0;
-
 
505
			}
-
 
506
		}
-
 
507
 
-
 
508
		DRM_ERROR("Invalid UVD handle 0x%x!\n", handle);
450
			return 0;
509
		return -ENOENT;
451
	}
510
 
452
 
511
	case 2:
453
	/* handle not found try to alloc a new one */
512
		/* it's a destroy msg, free the handle */
454
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
-
 
455
		if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
513
		for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
-
 
514
			atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
-
 
515
		radeon_bo_kunmap(bo);
456
			p->rdev->uvd.filp[i] = p->filp;
516
		return 0;
-
 
517
 
-
 
518
	default:
457
			p->rdev->uvd.img_size[i] = img_size;
519
 
458
			return 0;
520
		DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
459
		}
521
		return -EINVAL;
460
	}
522
	}
461
 
523
 
462
	DRM_ERROR("No more free UVD handles!\n");
524
	BUG();
463
	return -EINVAL;
525
	return -EINVAL;
464
}
526
}
465
 
527
 
466
static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
528
static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
467
			       int data0, int data1,
529
			       int data0, int data1,
468
			       unsigned buf_sizes[], bool *has_msg_cmd)
530
			       unsigned buf_sizes[], bool *has_msg_cmd)
469
{
531
{
470
	struct radeon_cs_chunk *relocs_chunk;
532
	struct radeon_cs_chunk *relocs_chunk;
471
	struct radeon_bo_list *reloc;
533
	struct radeon_bo_list *reloc;
472
	unsigned idx, cmd, offset;
534
	unsigned idx, cmd, offset;
473
	uint64_t start, end;
535
	uint64_t start, end;
474
	int r;
536
	int r;
475
 
537
 
476
	relocs_chunk = p->chunk_relocs;
538
	relocs_chunk = p->chunk_relocs;
477
	offset = radeon_get_ib_value(p, data0);
539
	offset = radeon_get_ib_value(p, data0);
478
	idx = radeon_get_ib_value(p, data1);
540
	idx = radeon_get_ib_value(p, data1);
479
	if (idx >= relocs_chunk->length_dw) {
541
	if (idx >= relocs_chunk->length_dw) {
480
		DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
542
		DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
481
			  idx, relocs_chunk->length_dw);
543
			  idx, relocs_chunk->length_dw);
482
		return -EINVAL;
544
		return -EINVAL;
483
	}
545
	}
484
 
546
 
485
	reloc = &p->relocs[(idx / 4)];
547
	reloc = &p->relocs[(idx / 4)];
486
	start = reloc->gpu_offset;
548
	start = reloc->gpu_offset;
487
	end = start + radeon_bo_size(reloc->robj);
549
	end = start + radeon_bo_size(reloc->robj);
488
	start += offset;
550
	start += offset;
489
 
551
 
490
	p->ib.ptr[data0] = start & 0xFFFFFFFF;
552
	p->ib.ptr[data0] = start & 0xFFFFFFFF;
491
	p->ib.ptr[data1] = start >> 32;
553
	p->ib.ptr[data1] = start >> 32;
492
 
554
 
493
	cmd = radeon_get_ib_value(p, p->idx) >> 1;
555
	cmd = radeon_get_ib_value(p, p->idx) >> 1;
494
 
556
 
495
	if (cmd < 0x4) {
557
	if (cmd < 0x4) {
496
		if (end <= start) {
558
		if (end <= start) {
497
			DRM_ERROR("invalid reloc offset %X!\n", offset);
559
			DRM_ERROR("invalid reloc offset %X!\n", offset);
498
			return -EINVAL;
560
			return -EINVAL;
499
		}
561
		}
500
		if ((end - start) < buf_sizes[cmd]) {
562
		if ((end - start) < buf_sizes[cmd]) {
501
			DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
563
			DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
502
				  (unsigned)(end - start), buf_sizes[cmd]);
564
				  (unsigned)(end - start), buf_sizes[cmd]);
503
			return -EINVAL;
565
			return -EINVAL;
504
		}
566
		}
505
 
567
 
506
	} else if (cmd != 0x100) {
568
	} else if (cmd != 0x100) {
507
		DRM_ERROR("invalid UVD command %X!\n", cmd);
569
		DRM_ERROR("invalid UVD command %X!\n", cmd);
508
		return -EINVAL;
570
		return -EINVAL;
509
	}
571
	}
510
 
572
 
511
	if ((start >> 28) != ((end - 1) >> 28)) {
573
	if ((start >> 28) != ((end - 1) >> 28)) {
512
		DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
574
		DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
513
			  start, end);
575
			  start, end);
514
		return -EINVAL;
576
		return -EINVAL;
515
	}
577
	}
516
 
578
 
517
	/* TODO: is this still necessary on NI+ ? */
579
	/* TODO: is this still necessary on NI+ ? */
518
	if ((cmd == 0 || cmd == 0x3) &&
580
	if ((cmd == 0 || cmd == 0x3) &&
519
	    (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
581
	    (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
520
		DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
582
		DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
521
			  start, end);
583
			  start, end);
522
		return -EINVAL;
584
		return -EINVAL;
523
	}
585
	}
524
 
586
 
525
	if (cmd == 0) {
587
	if (cmd == 0) {
526
		if (*has_msg_cmd) {
588
		if (*has_msg_cmd) {
527
			DRM_ERROR("More than one message in a UVD-IB!\n");
589
			DRM_ERROR("More than one message in a UVD-IB!\n");
528
			return -EINVAL;
590
			return -EINVAL;
529
		}
591
		}
530
		*has_msg_cmd = true;
592
		*has_msg_cmd = true;
531
		r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
593
		r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
532
		if (r)
594
		if (r)
533
			return r;
595
			return r;
534
	} else if (!*has_msg_cmd) {
596
	} else if (!*has_msg_cmd) {
535
		DRM_ERROR("Message needed before other commands are send!\n");
597
		DRM_ERROR("Message needed before other commands are send!\n");
536
		return -EINVAL;
598
		return -EINVAL;
537
	}
599
	}
538
 
600
 
539
	return 0;
601
	return 0;
540
}
602
}
541
 
603
 
542
static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
604
static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
543
			     struct radeon_cs_packet *pkt,
605
			     struct radeon_cs_packet *pkt,
544
			     int *data0, int *data1,
606
			     int *data0, int *data1,
545
			     unsigned buf_sizes[],
607
			     unsigned buf_sizes[],
546
			     bool *has_msg_cmd)
608
			     bool *has_msg_cmd)
547
{
609
{
548
	int i, r;
610
	int i, r;
549
 
611
 
550
	p->idx++;
612
	p->idx++;
551
	for (i = 0; i <= pkt->count; ++i) {
613
	for (i = 0; i <= pkt->count; ++i) {
552
		switch (pkt->reg + i*4) {
614
		switch (pkt->reg + i*4) {
553
		case UVD_GPCOM_VCPU_DATA0:
615
		case UVD_GPCOM_VCPU_DATA0:
554
			*data0 = p->idx;
616
			*data0 = p->idx;
555
			break;
617
			break;
556
		case UVD_GPCOM_VCPU_DATA1:
618
		case UVD_GPCOM_VCPU_DATA1:
557
			*data1 = p->idx;
619
			*data1 = p->idx;
558
			break;
620
			break;
559
		case UVD_GPCOM_VCPU_CMD:
621
		case UVD_GPCOM_VCPU_CMD:
560
			r = radeon_uvd_cs_reloc(p, *data0, *data1,
622
			r = radeon_uvd_cs_reloc(p, *data0, *data1,
561
						buf_sizes, has_msg_cmd);
623
						buf_sizes, has_msg_cmd);
562
			if (r)
624
			if (r)
563
				return r;
625
				return r;
564
			break;
626
			break;
565
		case UVD_ENGINE_CNTL:
627
		case UVD_ENGINE_CNTL:
566
			break;
628
			break;
567
		default:
629
		default:
568
			DRM_ERROR("Invalid reg 0x%X!\n",
630
			DRM_ERROR("Invalid reg 0x%X!\n",
569
				  pkt->reg + i*4);
631
				  pkt->reg + i*4);
570
			return -EINVAL;
632
			return -EINVAL;
571
		}
633
		}
572
		p->idx++;
634
		p->idx++;
573
	}
635
	}
574
	return 0;
636
	return 0;
575
}
637
}
576
 
638
 
577
int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
639
int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
578
{
640
{
579
	struct radeon_cs_packet pkt;
641
	struct radeon_cs_packet pkt;
580
	int r, data0 = 0, data1 = 0;
642
	int r, data0 = 0, data1 = 0;
581
 
643
 
582
	/* does the IB has a msg command */
644
	/* does the IB has a msg command */
583
	bool has_msg_cmd = false;
645
	bool has_msg_cmd = false;
584
 
646
 
585
	/* minimum buffer sizes */
647
	/* minimum buffer sizes */
586
	unsigned buf_sizes[] = {
648
	unsigned buf_sizes[] = {
587
		[0x00000000]	=	2048,
649
		[0x00000000]	=	2048,
588
		[0x00000001]	=	32 * 1024 * 1024,
650
		[0x00000001]	=	32 * 1024 * 1024,
589
		[0x00000002]	=	2048 * 1152 * 3,
651
		[0x00000002]	=	2048 * 1152 * 3,
590
		[0x00000003]	=	2048,
652
		[0x00000003]	=	2048,
591
	};
653
	};
592
 
654
 
593
	if (p->chunk_ib->length_dw % 16) {
655
	if (p->chunk_ib->length_dw % 16) {
594
		DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
656
		DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
595
			  p->chunk_ib->length_dw);
657
			  p->chunk_ib->length_dw);
596
		return -EINVAL;
658
		return -EINVAL;
597
	}
659
	}
598
 
660
 
599
	if (p->chunk_relocs == NULL) {
661
	if (p->chunk_relocs == NULL) {
600
		DRM_ERROR("No relocation chunk !\n");
662
		DRM_ERROR("No relocation chunk !\n");
601
		return -EINVAL;
663
		return -EINVAL;
602
	}
664
	}
603
 
665
 
604
 
666
 
605
	do {
667
	do {
606
		r = radeon_cs_packet_parse(p, &pkt, p->idx);
668
		r = radeon_cs_packet_parse(p, &pkt, p->idx);
607
		if (r)
669
		if (r)
608
			return r;
670
			return r;
609
		switch (pkt.type) {
671
		switch (pkt.type) {
610
		case RADEON_PACKET_TYPE0:
672
		case RADEON_PACKET_TYPE0:
611
			r = radeon_uvd_cs_reg(p, &pkt, &data0, &data1,
673
			r = radeon_uvd_cs_reg(p, &pkt, &data0, &data1,
612
					      buf_sizes, &has_msg_cmd);
674
					      buf_sizes, &has_msg_cmd);
613
			if (r)
675
			if (r)
614
				return r;
676
				return r;
615
			break;
677
			break;
616
		case RADEON_PACKET_TYPE2:
678
		case RADEON_PACKET_TYPE2:
617
			p->idx += pkt.count + 2;
679
			p->idx += pkt.count + 2;
618
			break;
680
			break;
619
		default:
681
		default:
620
			DRM_ERROR("Unknown packet type %d !\n", pkt.type);
682
			DRM_ERROR("Unknown packet type %d !\n", pkt.type);
621
			return -EINVAL;
683
			return -EINVAL;
622
		}
684
		}
623
	} while (p->idx < p->chunk_ib->length_dw);
685
	} while (p->idx < p->chunk_ib->length_dw);
624
 
686
 
625
	if (!has_msg_cmd) {
687
	if (!has_msg_cmd) {
626
		DRM_ERROR("UVD-IBs need a msg command!\n");
688
		DRM_ERROR("UVD-IBs need a msg command!\n");
627
		return -EINVAL;
689
		return -EINVAL;
628
	}
690
	}
629
 
691
 
630
	return 0;
692
	return 0;
631
}
693
}
632
 
694
 
633
static int radeon_uvd_send_msg(struct radeon_device *rdev,
695
static int radeon_uvd_send_msg(struct radeon_device *rdev,
634
			       int ring, uint64_t addr,
696
			       int ring, uint64_t addr,
635
			       struct radeon_fence **fence)
697
			       struct radeon_fence **fence)
636
{
698
{
637
	struct radeon_ib ib;
699
	struct radeon_ib ib;
638
	int i, r;
700
	int i, r;
639
 
701
 
640
	r = radeon_ib_get(rdev, ring, &ib, NULL, 64);
702
	r = radeon_ib_get(rdev, ring, &ib, NULL, 64);
641
	if (r)
703
	if (r)
642
		return r;
704
		return r;
643
 
705
 
644
	ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
706
	ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
645
	ib.ptr[1] = addr;
707
	ib.ptr[1] = addr;
646
	ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
708
	ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
647
	ib.ptr[3] = addr >> 32;
709
	ib.ptr[3] = addr >> 32;
648
	ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
710
	ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
649
	ib.ptr[5] = 0;
711
	ib.ptr[5] = 0;
650
	for (i = 6; i < 16; ++i)
712
	for (i = 6; i < 16; ++i)
651
		ib.ptr[i] = PACKET2(0);
713
		ib.ptr[i] = PACKET2(0);
652
	ib.length_dw = 16;
714
	ib.length_dw = 16;
653
 
715
 
654
	r = radeon_ib_schedule(rdev, &ib, NULL, false);
716
	r = radeon_ib_schedule(rdev, &ib, NULL, false);
655
 
717
 
656
	if (fence)
718
	if (fence)
657
		*fence = radeon_fence_ref(ib.fence);
719
		*fence = radeon_fence_ref(ib.fence);
658
 
720
 
659
	radeon_ib_free(rdev, &ib);
721
	radeon_ib_free(rdev, &ib);
660
	return r;
722
	return r;
661
}
723
}
662
 
724
 
663
/* multiple fence commands without any stream commands in between can
725
/* multiple fence commands without any stream commands in between can
664
   crash the vcpu so just try to emmit a dummy create/destroy msg to
726
   crash the vcpu so just try to emmit a dummy create/destroy msg to
665
   avoid this */
727
   avoid this */
666
int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
728
int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
667
			      uint32_t handle, struct radeon_fence **fence)
729
			      uint32_t handle, struct radeon_fence **fence)
668
{
730
{
669
	/* we use the last page of the vcpu bo for the UVD message */
731
	/* we use the last page of the vcpu bo for the UVD message */
670
	uint64_t offs = radeon_bo_size(rdev->uvd.vcpu_bo) -
732
	uint64_t offs = radeon_bo_size(rdev->uvd.vcpu_bo) -
671
		RADEON_GPU_PAGE_SIZE;
733
		RADEON_GPU_PAGE_SIZE;
672
 
734
 
673
	uint32_t *msg = rdev->uvd.cpu_addr + offs;
735
	uint32_t *msg = rdev->uvd.cpu_addr + offs;
674
	uint64_t addr = rdev->uvd.gpu_addr + offs;
736
	uint64_t addr = rdev->uvd.gpu_addr + offs;
675
 
737
 
676
	int r, i;
738
	int r, i;
677
 
739
 
678
	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, true);
740
	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, true);
679
	if (r)
741
	if (r)
680
		return r;
742
		return r;
681
 
743
 
682
	/* stitch together an UVD create msg */
744
	/* stitch together an UVD create msg */
683
	msg[0] = cpu_to_le32(0x00000de4);
745
	msg[0] = cpu_to_le32(0x00000de4);
684
	msg[1] = cpu_to_le32(0x00000000);
746
	msg[1] = cpu_to_le32(0x00000000);
685
	msg[2] = cpu_to_le32(handle);
747
	msg[2] = cpu_to_le32(handle);
686
	msg[3] = cpu_to_le32(0x00000000);
748
	msg[3] = cpu_to_le32(0x00000000);
687
	msg[4] = cpu_to_le32(0x00000000);
749
	msg[4] = cpu_to_le32(0x00000000);
688
	msg[5] = cpu_to_le32(0x00000000);
750
	msg[5] = cpu_to_le32(0x00000000);
689
	msg[6] = cpu_to_le32(0x00000000);
751
	msg[6] = cpu_to_le32(0x00000000);
690
	msg[7] = cpu_to_le32(0x00000780);
752
	msg[7] = cpu_to_le32(0x00000780);
691
	msg[8] = cpu_to_le32(0x00000440);
753
	msg[8] = cpu_to_le32(0x00000440);
692
	msg[9] = cpu_to_le32(0x00000000);
754
	msg[9] = cpu_to_le32(0x00000000);
693
	msg[10] = cpu_to_le32(0x01b37000);
755
	msg[10] = cpu_to_le32(0x01b37000);
694
	for (i = 11; i < 1024; ++i)
756
	for (i = 11; i < 1024; ++i)
695
		msg[i] = cpu_to_le32(0x0);
757
		msg[i] = cpu_to_le32(0x0);
696
 
758
 
697
	r = radeon_uvd_send_msg(rdev, ring, addr, fence);
759
	r = radeon_uvd_send_msg(rdev, ring, addr, fence);
698
	radeon_bo_unreserve(rdev->uvd.vcpu_bo);
760
	radeon_bo_unreserve(rdev->uvd.vcpu_bo);
699
	return r;
761
	return r;
700
}
762
}
701
 
763
 
702
int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
764
int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
703
			       uint32_t handle, struct radeon_fence **fence)
765
			       uint32_t handle, struct radeon_fence **fence)
704
{
766
{
705
	/* we use the last page of the vcpu bo for the UVD message */
767
	/* we use the last page of the vcpu bo for the UVD message */
706
	uint64_t offs = radeon_bo_size(rdev->uvd.vcpu_bo) -
768
	uint64_t offs = radeon_bo_size(rdev->uvd.vcpu_bo) -
707
		RADEON_GPU_PAGE_SIZE;
769
		RADEON_GPU_PAGE_SIZE;
708
 
770
 
709
	uint32_t *msg = rdev->uvd.cpu_addr + offs;
771
	uint32_t *msg = rdev->uvd.cpu_addr + offs;
710
	uint64_t addr = rdev->uvd.gpu_addr + offs;
772
	uint64_t addr = rdev->uvd.gpu_addr + offs;
711
 
773
 
712
	int r, i;
774
	int r, i;
713
 
775
 
714
	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, true);
776
	r = radeon_bo_reserve(rdev->uvd.vcpu_bo, true);
715
	if (r)
777
	if (r)
716
		return r;
778
		return r;
717
 
779
 
718
	/* stitch together an UVD destroy msg */
780
	/* stitch together an UVD destroy msg */
719
	msg[0] = cpu_to_le32(0x00000de4);
781
	msg[0] = cpu_to_le32(0x00000de4);
720
	msg[1] = cpu_to_le32(0x00000002);
782
	msg[1] = cpu_to_le32(0x00000002);
721
	msg[2] = cpu_to_le32(handle);
783
	msg[2] = cpu_to_le32(handle);
722
	msg[3] = cpu_to_le32(0x00000000);
784
	msg[3] = cpu_to_le32(0x00000000);
723
	for (i = 4; i < 1024; ++i)
785
	for (i = 4; i < 1024; ++i)
724
		msg[i] = cpu_to_le32(0x0);
786
		msg[i] = cpu_to_le32(0x0);
725
 
787
 
726
	r = radeon_uvd_send_msg(rdev, ring, addr, fence);
788
	r = radeon_uvd_send_msg(rdev, ring, addr, fence);
727
	radeon_bo_unreserve(rdev->uvd.vcpu_bo);
789
	radeon_bo_unreserve(rdev->uvd.vcpu_bo);
728
	return r;
790
	return r;
729
}
791
}
730
 
792
 
731
/**
793
/**
732
 * radeon_uvd_count_handles - count number of open streams
794
 * radeon_uvd_count_handles - count number of open streams
733
 *
795
 *
734
 * @rdev: radeon_device pointer
796
 * @rdev: radeon_device pointer
735
 * @sd: number of SD streams
797
 * @sd: number of SD streams
736
 * @hd: number of HD streams
798
 * @hd: number of HD streams
737
 *
799
 *
738
 * Count the number of open SD/HD streams as a hint for power mangement
800
 * Count the number of open SD/HD streams as a hint for power mangement
739
 */
801
 */
740
static void radeon_uvd_count_handles(struct radeon_device *rdev,
802
static void radeon_uvd_count_handles(struct radeon_device *rdev,
741
				     unsigned *sd, unsigned *hd)
803
				     unsigned *sd, unsigned *hd)
742
{
804
{
743
	unsigned i;
805
	unsigned i;
744
 
806
 
745
	*sd = 0;
807
	*sd = 0;
746
	*hd = 0;
808
	*hd = 0;
747
 
809
 
748
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
810
	for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
749
		if (!atomic_read(&rdev->uvd.handles[i]))
811
		if (!atomic_read(&rdev->uvd.handles[i]))
750
			continue;
812
			continue;
751
 
813
 
752
		if (rdev->uvd.img_size[i] >= 720*576)
814
		if (rdev->uvd.img_size[i] >= 720*576)
753
			++(*hd);
815
			++(*hd);
754
		else
816
		else
755
			++(*sd);
817
			++(*sd);
756
	}
818
	}
757
}
819
}
758
 
820
 
759
static void radeon_uvd_idle_work_handler(struct work_struct *work)
821
static void radeon_uvd_idle_work_handler(struct work_struct *work)
760
{
822
{
761
	struct radeon_device *rdev =
823
	struct radeon_device *rdev =
762
		container_of(work, struct radeon_device, uvd.idle_work.work);
824
		container_of(work, struct radeon_device, uvd.idle_work.work);
763
 
825
 
764
	if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0) {
826
	if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0) {
765
		if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
827
		if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
766
			radeon_uvd_count_handles(rdev, &rdev->pm.dpm.sd,
828
			radeon_uvd_count_handles(rdev, &rdev->pm.dpm.sd,
767
						 &rdev->pm.dpm.hd);
829
						 &rdev->pm.dpm.hd);
768
			radeon_dpm_enable_uvd(rdev, false);
830
			radeon_dpm_enable_uvd(rdev, false);
769
		} else {
831
		} else {
770
			radeon_set_uvd_clocks(rdev, 0, 0);
832
			radeon_set_uvd_clocks(rdev, 0, 0);
771
		}
833
		}
772
	} else {
834
	} else {
773
		schedule_delayed_work(&rdev->uvd.idle_work,
835
		schedule_delayed_work(&rdev->uvd.idle_work,
774
				      msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
836
				      msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
775
	}
837
	}
776
}
838
}
777
 
839
 
778
void radeon_uvd_note_usage(struct radeon_device *rdev)
840
void radeon_uvd_note_usage(struct radeon_device *rdev)
779
{
841
{
780
	bool streams_changed = false;
842
	bool streams_changed = false;
781
	bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
843
	bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
782
	set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
844
	set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
783
					    msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
845
					    msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
784
 
846
 
785
	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
847
	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
786
		unsigned hd = 0, sd = 0;
848
		unsigned hd = 0, sd = 0;
787
		radeon_uvd_count_handles(rdev, &sd, &hd);
849
		radeon_uvd_count_handles(rdev, &sd, &hd);
788
		if ((rdev->pm.dpm.sd != sd) ||
850
		if ((rdev->pm.dpm.sd != sd) ||
789
		    (rdev->pm.dpm.hd != hd)) {
851
		    (rdev->pm.dpm.hd != hd)) {
790
			rdev->pm.dpm.sd = sd;
852
			rdev->pm.dpm.sd = sd;
791
			rdev->pm.dpm.hd = hd;
853
			rdev->pm.dpm.hd = hd;
792
			/* disable this for now */
854
			/* disable this for now */
793
			/*streams_changed = true;*/
855
			/*streams_changed = true;*/
794
		}
856
		}
795
	}
857
	}
796
 
858
 
797
	if (set_clocks || streams_changed) {
859
	if (set_clocks || streams_changed) {
798
		if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
860
		if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
799
			radeon_dpm_enable_uvd(rdev, true);
861
			radeon_dpm_enable_uvd(rdev, true);
800
		} else {
862
		} else {
801
			radeon_set_uvd_clocks(rdev, 53300, 40000);
863
			radeon_set_uvd_clocks(rdev, 53300, 40000);
802
		}
864
		}
803
	}
865
	}
804
}
866
}
805
 
867
 
806
static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
868
static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
807
					      unsigned target_freq,
869
					      unsigned target_freq,
808
					      unsigned pd_min,
870
					      unsigned pd_min,
809
					      unsigned pd_even)
871
					      unsigned pd_even)
810
{
872
{
811
	unsigned post_div = vco_freq / target_freq;
873
	unsigned post_div = vco_freq / target_freq;
812
 
874
 
813
	/* adjust to post divider minimum value */
875
	/* adjust to post divider minimum value */
814
	if (post_div < pd_min)
876
	if (post_div < pd_min)
815
		post_div = pd_min;
877
		post_div = pd_min;
816
 
878
 
817
	/* we alway need a frequency less than or equal the target */
879
	/* we alway need a frequency less than or equal the target */
818
	if ((vco_freq / post_div) > target_freq)
880
	if ((vco_freq / post_div) > target_freq)
819
		post_div += 1;
881
		post_div += 1;
820
 
882
 
821
	/* post dividers above a certain value must be even */
883
	/* post dividers above a certain value must be even */
822
	if (post_div > pd_even && post_div % 2)
884
	if (post_div > pd_even && post_div % 2)
823
		post_div += 1;
885
		post_div += 1;
824
 
886
 
825
	return post_div;
887
	return post_div;
826
}
888
}
827
 
889
 
828
/**
890
/**
829
 * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
891
 * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
830
 *
892
 *
831
 * @rdev: radeon_device pointer
893
 * @rdev: radeon_device pointer
832
 * @vclk: wanted VCLK
894
 * @vclk: wanted VCLK
833
 * @dclk: wanted DCLK
895
 * @dclk: wanted DCLK
834
 * @vco_min: minimum VCO frequency
896
 * @vco_min: minimum VCO frequency
835
 * @vco_max: maximum VCO frequency
897
 * @vco_max: maximum VCO frequency
836
 * @fb_factor: factor to multiply vco freq with
898
 * @fb_factor: factor to multiply vco freq with
837
 * @fb_mask: limit and bitmask for feedback divider
899
 * @fb_mask: limit and bitmask for feedback divider
838
 * @pd_min: post divider minimum
900
 * @pd_min: post divider minimum
839
 * @pd_max: post divider maximum
901
 * @pd_max: post divider maximum
840
 * @pd_even: post divider must be even above this value
902
 * @pd_even: post divider must be even above this value
841
 * @optimal_fb_div: resulting feedback divider
903
 * @optimal_fb_div: resulting feedback divider
842
 * @optimal_vclk_div: resulting vclk post divider
904
 * @optimal_vclk_div: resulting vclk post divider
843
 * @optimal_dclk_div: resulting dclk post divider
905
 * @optimal_dclk_div: resulting dclk post divider
844
 *
906
 *
845
 * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
907
 * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
846
 * Returns zero on success -EINVAL on error.
908
 * Returns zero on success -EINVAL on error.
847
 */
909
 */
848
int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
910
int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
849
				  unsigned vclk, unsigned dclk,
911
				  unsigned vclk, unsigned dclk,
850
				  unsigned vco_min, unsigned vco_max,
912
				  unsigned vco_min, unsigned vco_max,
851
				  unsigned fb_factor, unsigned fb_mask,
913
				  unsigned fb_factor, unsigned fb_mask,
852
				  unsigned pd_min, unsigned pd_max,
914
				  unsigned pd_min, unsigned pd_max,
853
				  unsigned pd_even,
915
				  unsigned pd_even,
854
				  unsigned *optimal_fb_div,
916
				  unsigned *optimal_fb_div,
855
				  unsigned *optimal_vclk_div,
917
				  unsigned *optimal_vclk_div,
856
				  unsigned *optimal_dclk_div)
918
				  unsigned *optimal_dclk_div)
857
{
919
{
858
	unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
920
	unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
859
 
921
 
860
	/* start off with something large */
922
	/* start off with something large */
861
	unsigned optimal_score = ~0;
923
	unsigned optimal_score = ~0;
862
 
924
 
863
	/* loop through vco from low to high */
925
	/* loop through vco from low to high */
864
	vco_min = max(max(vco_min, vclk), dclk);
926
	vco_min = max(max(vco_min, vclk), dclk);
865
	for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
927
	for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
866
 
928
 
867
		uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
929
		uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
868
		unsigned vclk_div, dclk_div, score;
930
		unsigned vclk_div, dclk_div, score;
869
 
931
 
870
		do_div(fb_div, ref_freq);
932
		do_div(fb_div, ref_freq);
871
 
933
 
872
		/* fb div out of range ? */
934
		/* fb div out of range ? */
873
		if (fb_div > fb_mask)
935
		if (fb_div > fb_mask)
874
			break; /* it can oly get worse */
936
			break; /* it can oly get worse */
875
 
937
 
876
		fb_div &= fb_mask;
938
		fb_div &= fb_mask;
877
 
939
 
878
		/* calc vclk divider with current vco freq */
940
		/* calc vclk divider with current vco freq */
879
		vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
941
		vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
880
							 pd_min, pd_even);
942
							 pd_min, pd_even);
881
		if (vclk_div > pd_max)
943
		if (vclk_div > pd_max)
882
			break; /* vco is too big, it has to stop */
944
			break; /* vco is too big, it has to stop */
883
 
945
 
884
		/* calc dclk divider with current vco freq */
946
		/* calc dclk divider with current vco freq */
885
		dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
947
		dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
886
							 pd_min, pd_even);
948
							 pd_min, pd_even);
887
		if (vclk_div > pd_max)
949
		if (vclk_div > pd_max)
888
			break; /* vco is too big, it has to stop */
950
			break; /* vco is too big, it has to stop */
889
 
951
 
890
		/* calc score with current vco freq */
952
		/* calc score with current vco freq */
891
		score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
953
		score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
892
 
954
 
893
		/* determine if this vco setting is better than current optimal settings */
955
		/* determine if this vco setting is better than current optimal settings */
894
		if (score < optimal_score) {
956
		if (score < optimal_score) {
895
			*optimal_fb_div = fb_div;
957
			*optimal_fb_div = fb_div;
896
			*optimal_vclk_div = vclk_div;
958
			*optimal_vclk_div = vclk_div;
897
			*optimal_dclk_div = dclk_div;
959
			*optimal_dclk_div = dclk_div;
898
			optimal_score = score;
960
			optimal_score = score;
899
			if (optimal_score == 0)
961
			if (optimal_score == 0)
900
				break; /* it can't get better than this */
962
				break; /* it can't get better than this */
901
		}
963
		}
902
	}
964
	}
903
 
965
 
904
	/* did we found a valid setup ? */
966
	/* did we found a valid setup ? */
905
	if (optimal_score == ~0)
967
	if (optimal_score == ~0)
906
		return -EINVAL;
968
		return -EINVAL;
907
 
969
 
908
	return 0;
970
	return 0;
909
}
971
}
910
 
972
 
911
int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
973
int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
912
				unsigned cg_upll_func_cntl)
974
				unsigned cg_upll_func_cntl)
913
{
975
{
914
	unsigned i;
976
	unsigned i;
915
 
977
 
916
	/* make sure UPLL_CTLREQ is deasserted */
978
	/* make sure UPLL_CTLREQ is deasserted */
917
	WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
979
	WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
918
 
980
 
919
	mdelay(10);
981
	mdelay(10);
920
 
982
 
921
	/* assert UPLL_CTLREQ */
983
	/* assert UPLL_CTLREQ */
922
	WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
984
	WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
923
 
985
 
924
	/* wait for CTLACK and CTLACK2 to get asserted */
986
	/* wait for CTLACK and CTLACK2 to get asserted */
925
	for (i = 0; i < 100; ++i) {
987
	for (i = 0; i < 100; ++i) {
926
		uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
988
		uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
927
		if ((RREG32(cg_upll_func_cntl) & mask) == mask)
989
		if ((RREG32(cg_upll_func_cntl) & mask) == mask)
928
			break;
990
			break;
929
		mdelay(10);
991
		mdelay(10);
930
	}
992
	}
931
 
993
 
932
	/* deassert UPLL_CTLREQ */
994
	/* deassert UPLL_CTLREQ */
933
	WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
995
	WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
934
 
996
 
935
	if (i == 100) {
997
	if (i == 100) {
936
		DRM_ERROR("Timeout setting UVD clocks!\n");
998
		DRM_ERROR("Timeout setting UVD clocks!\n");
937
		return -ETIMEDOUT;
999
		return -ETIMEDOUT;
938
	}
1000
	}
939
 
1001
 
940
	return 0;
1002
	return 0;
941
}
1003
}