Subversion Repositories Kolibri OS

Rev

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