Subversion Repositories Kolibri OS

Rev

Rev 6104 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1126 serge 1
/*
2
 * Copyright © 2007 David Airlie
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 (including the next
12
 * paragraph) shall be included in all copies or substantial portions of the
13
 * Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 * Authors:
24
 *     David Airlie
25
 */
1179 serge 26
#include 
1986 serge 27
#include 
1179 serge 28
#include 
1126 serge 29
 
2997 Serge 30
#include 
31
#include 
32
#include 
33
#include 
1126 serge 34
#include "radeon.h"
35
 
2997 Serge 36
#include 
1179 serge 37
 
5078 serge 38
struct drm_framebuffer *main_fb;
39
struct drm_gem_object  *main_fb_obj;
1126 serge 40
 
1963 serge 41
/* object hierarchy -
42
   this contains a helper + a radeon fb
43
   the helper contains a pointer to radeon framebuffer baseclass.
44
*/
45
struct radeon_fbdev {
6104 serge 46
	struct drm_fb_helper helper;
1963 serge 47
	struct radeon_framebuffer rfb;
6104 serge 48
	struct radeon_device *rdev;
1126 serge 49
};
50
 
51
static struct fb_ops radeonfb_ops = {
2997 Serge 52
	.owner = THIS_MODULE,
1179 serge 53
	.fb_check_var = drm_fb_helper_check_var,
54
	.fb_set_par = drm_fb_helper_set_par,
55
//	.fb_fillrect = cfb_fillrect,
56
//	.fb_copyarea = cfb_copyarea,
57
//	.fb_imageblit = cfb_imageblit,
58
//	.fb_pan_display = drm_fb_helper_pan_display,
59
	.fb_blank = drm_fb_helper_blank,
1221 serge 60
	.fb_setcmap = drm_fb_helper_setcmap,
1126 serge 61
};
62
 
63
 
1233 serge 64
int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
1126 serge 65
{
66
	int aligned = width;
1179 serge 67
	int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
1126 serge 68
	int pitch_mask = 0;
69
 
70
	switch (bpp / 8) {
71
	case 1:
72
		pitch_mask = align_large ? 255 : 127;
73
		break;
74
	case 2:
75
		pitch_mask = align_large ? 127 : 31;
76
		break;
77
	case 3:
78
	case 4:
79
		pitch_mask = align_large ? 63 : 15;
80
		break;
81
	}
82
 
83
	aligned += pitch_mask;
84
	aligned &= ~pitch_mask;
85
	return aligned;
86
}
87
 
5078 serge 88
static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
89
{
90
	struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
91
	int ret;
1179 serge 92
 
5078 serge 93
	ret = radeon_bo_reserve(rbo, false);
94
	if (likely(ret == 0)) {
95
		radeon_bo_kunmap(rbo);
96
		radeon_bo_unpin(rbo);
97
		radeon_bo_unreserve(rbo);
98
	}
99
	drm_gem_object_unreference_unlocked(gobj);
100
}
101
 
102
static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
6104 serge 103
					 struct drm_mode_fb_cmd2 *mode_cmd,
104
					 struct drm_gem_object **gobj_p)
5078 serge 105
{
6104 serge 106
	struct radeon_device *rdev = rfbdev->rdev;
107
	struct drm_gem_object *gobj = NULL;
108
	struct radeon_bo *rbo = NULL;
109
	bool fb_tiled = false; /* useful for testing */
110
	u32 tiling_flags = 0;
111
	int ret;
112
	int aligned_size, size;
113
	int height = mode_cmd->height;
114
	u32 bpp, depth;
5078 serge 115
 
6104 serge 116
	drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
5078 serge 117
 
6104 serge 118
	/* need to align pitch with crtc limits */
119
	mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, bpp,
120
						  fb_tiled) * ((bpp + 1) / 8);
5078 serge 121
 
6104 serge 122
	if (rdev->family >= CHIP_R600)
123
		height = ALIGN(mode_cmd->height, 8);
124
	size = mode_cmd->pitches[0] * height;
125
	aligned_size = ALIGN(size, PAGE_SIZE);
5078 serge 126
 
127
	rbo = rdev->stollen_vga_memory;
128
	gobj = &rbo->gem_base;
129
	mutex_lock(&rdev->gem.mutex);
130
	list_add_tail(&rbo->list, &rdev->gem.objects);
131
	mutex_unlock(&rdev->gem.mutex);
132
 
6104 serge 133
	rbo = gem_to_radeon_bo(gobj);
5078 serge 134
 
6104 serge 135
	if (fb_tiled)
136
		tiling_flags = RADEON_TILING_MACRO;
5078 serge 137
 
138
#ifdef __BIG_ENDIAN
139
	switch (bpp) {
140
	case 32:
141
		tiling_flags |= RADEON_TILING_SWAP_32BIT;
142
		break;
143
	case 16:
144
		tiling_flags |= RADEON_TILING_SWAP_16BIT;
145
	default:
146
		break;
147
	}
148
#endif
149
 
150
//    if (tiling_flags) {
151
//        ret = radeon_bo_set_tiling_flags(rbo,
152
//                         tiling_flags | RADEON_TILING_SURFACE,
153
//                         mode_cmd->pitches[0]);
154
//        if (ret)
155
//            dev_err(rdev->dev, "FB failed to set tiling flags\n");
156
//    }
157
 
158
 
159
	ret = radeon_bo_reserve(rbo, false);
160
	if (unlikely(ret != 0))
161
		goto out_unref;
162
	/* Only 27 bit offset for legacy CRTC */
163
	ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
164
				       ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
165
				       NULL);
166
	if (ret) {
167
		radeon_bo_unreserve(rbo);
168
		goto out_unref;
169
	}
170
	radeon_bo_unreserve(rbo);
171
	if (ret) {
172
		goto out_unref;
173
	}
174
 
6104 serge 175
	*gobj_p = gobj;
176
	return 0;
5078 serge 177
out_unref:
178
	radeonfb_destroy_pinned_object(gobj);
179
	*gobj_p = NULL;
180
	return ret;
181
}
182
 
183
static int radeonfb_create(struct drm_fb_helper *helper,
1963 serge 184
			   struct drm_fb_helper_surface_size *sizes)
185
{
5271 serge 186
	struct radeon_fbdev *rfbdev =
187
		container_of(helper, struct radeon_fbdev, helper);
1963 serge 188
	struct radeon_device *rdev = rfbdev->rdev;
189
	struct fb_info *info;
190
	struct drm_framebuffer *fb = NULL;
2997 Serge 191
	struct drm_mode_fb_cmd2 mode_cmd;
1963 serge 192
	struct drm_gem_object *gobj = NULL;
193
	struct radeon_bo *rbo = NULL;
194
	int ret;
195
	unsigned long tmp;
1126 serge 196
 
1963 serge 197
	mode_cmd.width = sizes->surface_width;
198
	mode_cmd.height = sizes->surface_height;
199
 
200
	/* avivo can't scanout real 24bpp */
201
	if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
202
		sizes->surface_bpp = 32;
203
 
2997 Serge 204
	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
205
							  sizes->surface_depth);
1963 serge 206
 
2997 Serge 207
	ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
208
	if (ret) {
209
		DRM_ERROR("failed to create fbcon object %d\n", ret);
210
		return ret;
211
	}
212
 
1986 serge 213
	rbo = gem_to_radeon_bo(gobj);
1963 serge 214
 
215
	/* okay we have an object now allocate the framebuffer */
6104 serge 216
	info = drm_fb_helper_alloc_fbi(helper);
217
	if (IS_ERR(info)) {
218
		ret = PTR_ERR(info);
1126 serge 219
		goto out_unref;
220
	}
1179 serge 221
 
1963 serge 222
	info->par = rfbdev;
6104 serge 223
	info->skip_vt_switch = true;
1126 serge 224
 
2997 Serge 225
	ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
226
	if (ret) {
5078 serge 227
		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
6104 serge 228
		goto out_destroy_fbi;
2997 Serge 229
	}
1963 serge 230
 
231
	fb = &rfbdev->rfb.base;
232
 
233
	/* setup helper */
234
	rfbdev->helper.fb = fb;
235
 
236
//   memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
237
 
1179 serge 238
	strcpy(info->fix.id, "radeondrmfb");
1126 serge 239
 
2997 Serge 240
	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
1179 serge 241
 
1963 serge 242
	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
1179 serge 243
	info->fbops = &radeonfb_ops;
244
 
1963 serge 245
	tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
1126 serge 246
	info->fix.smem_start = rdev->mc.aper_base + tmp;
1963 serge 247
	info->fix.smem_len = radeon_bo_size(rbo);
248
	info->screen_base = rbo->kptr;
249
	info->screen_size = radeon_bo_size(rbo);
1179 serge 250
 
1963 serge 251
	drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
1179 serge 252
 
253
	/* setup aperture base/size for vesafb takeover */
1963 serge 254
	info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
255
	info->apertures->ranges[0].size = rdev->mc.aper_size;
1179 serge 256
 
2997 Serge 257
	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
1963 serge 258
 
5078 serge 259
 
1126 serge 260
	DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
261
	DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
1963 serge 262
	DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
1126 serge 263
	DRM_INFO("fb depth is %d\n", fb->depth);
2997 Serge 264
	DRM_INFO("   pitch is %d\n", fb->pitches[0]);
1126 serge 265
 
5078 serge 266
    main_fb = fb;
267
    main_fb_obj = gobj;
1986 serge 268
 
6104 serge 269
	return 0;
1126 serge 270
 
6104 serge 271
out_destroy_fbi:
272
//   drm_fb_helper_release_fbi(helper);
1126 serge 273
out_unref:
1404 serge 274
	if (rbo) {
1963 serge 275
 
1126 serge 276
	}
277
	if (fb && ret) {
278
		kfree(fb);
279
	}
280
	return ret;
281
}
282
 
1963 serge 283
 
284
static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
285
{
286
	struct radeon_framebuffer *rfb = &rfbdev->rfb;
287
 
6104 serge 288
//	drm_fb_helper_unregister_fbi(&rfbdev->helper);
289
//	drm_fb_helper_release_fbi(&rfbdev->helper);
1963 serge 290
 
291
	if (rfb->obj) {
292
		rfb->obj = NULL;
293
	}
294
//   drm_fb_helper_fini(&rfbdev->helper);
295
	drm_framebuffer_cleanup(&rfb->base);
296
 
297
	return 0;
298
}
299
 
5078 serge 300
static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
1963 serge 301
	.gamma_set = radeon_crtc_fb_gamma_set,
302
	.gamma_get = radeon_crtc_fb_gamma_get,
5078 serge 303
	.fb_probe = radeonfb_create,
1963 serge 304
};
305
 
306
int radeon_fbdev_init(struct radeon_device *rdev)
307
{
308
	struct radeon_fbdev *rfbdev;
1404 serge 309
	int bpp_sel = 32;
1963 serge 310
	int ret;
1404 serge 311
 
312
	/* select 8 bpp console on RN50 or 16MB cards */
313
	if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
314
		bpp_sel = 8;
315
 
1963 serge 316
	rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
317
	if (!rfbdev)
318
		return -ENOMEM;
1126 serge 319
 
1963 serge 320
	rfbdev->rdev = rdev;
321
	rdev->mode_info.rfbdev = rfbdev;
1126 serge 322
 
5078 serge 323
	drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
324
			      &radeon_fb_helper_funcs);
325
 
1963 serge 326
	ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
327
				 rdev->num_crtc,
328
				 RADEONFB_CONN_LIMIT);
6104 serge 329
	if (ret)
330
		goto free;
1126 serge 331
 
6104 serge 332
	ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
333
	if (ret)
334
		goto fini;
1179 serge 335
 
5078 serge 336
	/* disable all the possible outputs/crtcs before entering KMS mode */
337
	drm_helper_disable_unused_functions(rdev->ddev);
1986 serge 338
 
6104 serge 339
	ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
340
	if (ret)
341
		goto fini;
1963 serge 342
 
1126 serge 343
	return 0;
6104 serge 344
 
345
fini:
346
//   drm_fb_helper_fini(&rfbdev->helper);
347
free:
348
	kfree(rfbdev);
349
	return ret;
1126 serge 350
}
1963 serge 351
 
352
void radeon_fbdev_fini(struct radeon_device *rdev)
353
{
354
	if (!rdev->mode_info.rfbdev)
355
		return;
356
 
357
	radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
358
	kfree(rdev->mode_info.rfbdev);
359
	rdev->mode_info.rfbdev = NULL;
360
}
361
 
362
bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
363
{
1986 serge 364
	if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
1963 serge 365
		return true;
366
	return false;
367
}
6104 serge 368
 
369
void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
370
{
371
	drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
372
}
373
 
374
void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
375
{
376
	drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
377
}