Subversion Repositories Kolibri OS

Rev

Rev 5078 | 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 {
1179 serge 46
    struct drm_fb_helper        helper;
1963 serge 47
	struct radeon_framebuffer rfb;
48
	struct list_head fbdev_list;
1126 serge 49
	struct radeon_device		*rdev;
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,
104
                     struct drm_mode_fb_cmd2 *mode_cmd,
105
                     struct drm_gem_object **gobj_p)
106
{
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;
116
 
117
    drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
118
 
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);
122
 
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);
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
 
134
    rbo = gem_to_radeon_bo(gobj);
135
 
136
    if (fb_tiled)
137
        tiling_flags = RADEON_TILING_MACRO;
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
 
176
    *gobj_p = gobj;
177
    return 0;
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
	struct device *device = &rdev->pdev->dev;
196
	int ret;
197
	unsigned long tmp;
1126 serge 198
 
1963 serge 199
	mode_cmd.width = sizes->surface_width;
200
	mode_cmd.height = sizes->surface_height;
201
 
202
	/* avivo can't scanout real 24bpp */
203
	if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
204
		sizes->surface_bpp = 32;
205
 
2997 Serge 206
	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
207
							  sizes->surface_depth);
1963 serge 208
 
2997 Serge 209
	ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
210
	if (ret) {
211
		DRM_ERROR("failed to create fbcon object %d\n", ret);
212
		return ret;
213
	}
214
 
1986 serge 215
	rbo = gem_to_radeon_bo(gobj);
1963 serge 216
 
217
	/* okay we have an object now allocate the framebuffer */
218
	info = framebuffer_alloc(0, device);
1126 serge 219
	if (info == NULL) {
220
		ret = -ENOMEM;
221
		goto out_unref;
222
	}
1179 serge 223
 
1963 serge 224
	info->par = rfbdev;
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);
2997 Serge 229
		goto out_unref;
230
	}
1963 serge 231
 
232
	fb = &rfbdev->rfb.base;
233
 
234
	/* setup helper */
235
	rfbdev->helper.fb = fb;
236
	rfbdev->helper.fbdev = info;
237
 
238
//   memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
239
 
1179 serge 240
	strcpy(info->fix.id, "radeondrmfb");
1126 serge 241
 
2997 Serge 242
	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
1179 serge 243
 
1963 serge 244
	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
1179 serge 245
	info->fbops = &radeonfb_ops;
246
 
1963 serge 247
	tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
1126 serge 248
	info->fix.smem_start = rdev->mc.aper_base + tmp;
1963 serge 249
	info->fix.smem_len = radeon_bo_size(rbo);
250
	info->screen_base = rbo->kptr;
251
	info->screen_size = radeon_bo_size(rbo);
1179 serge 252
 
1963 serge 253
	drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
1179 serge 254
 
255
	/* setup aperture base/size for vesafb takeover */
1963 serge 256
	info->apertures = alloc_apertures(1);
257
	if (!info->apertures) {
258
		ret = -ENOMEM;
259
		goto out_unref;
260
	}
261
	info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
262
	info->apertures->ranges[0].size = rdev->mc.aper_size;
1179 serge 263
 
2997 Serge 264
	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
1963 serge 265
 
5078 serge 266
 
1126 serge 267
	DRM_INFO("fb mappable at 0x%lX\n",  info->fix.smem_start);
268
	DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
1963 serge 269
	DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
1126 serge 270
	DRM_INFO("fb depth is %d\n", fb->depth);
2997 Serge 271
	DRM_INFO("   pitch is %d\n", fb->pitches[0]);
1126 serge 272
 
5078 serge 273
    main_fb = fb;
274
    main_fb_obj = gobj;
1986 serge 275
 
1963 serge 276
    return 0;
1126 serge 277
 
278
out_unref:
1404 serge 279
	if (rbo) {
1963 serge 280
 
1126 serge 281
	}
282
	if (fb && ret) {
283
		kfree(fb);
284
	}
285
	return ret;
286
}
287
 
1963 serge 288
 
289
static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
290
{
291
	struct fb_info *info;
292
	struct radeon_framebuffer *rfb = &rfbdev->rfb;
293
 
294
	if (rfbdev->helper.fbdev) {
295
		info = rfbdev->helper.fbdev;
296
 
297
//       unregister_framebuffer(info);
298
//       framebuffer_release(info);
299
	}
300
 
301
	if (rfb->obj) {
302
		rfb->obj = NULL;
303
	}
304
//   drm_fb_helper_fini(&rfbdev->helper);
305
	drm_framebuffer_cleanup(&rfb->base);
306
 
307
	return 0;
308
}
309
 
5078 serge 310
static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
1963 serge 311
	.gamma_set = radeon_crtc_fb_gamma_set,
312
	.gamma_get = radeon_crtc_fb_gamma_get,
5078 serge 313
	.fb_probe = radeonfb_create,
1963 serge 314
};
315
 
316
int radeon_fbdev_init(struct radeon_device *rdev)
317
{
318
	struct radeon_fbdev *rfbdev;
1404 serge 319
	int bpp_sel = 32;
1963 serge 320
	int ret;
5078 serge 321
ENTER();
1404 serge 322
 
323
	/* select 8 bpp console on RN50 or 16MB cards */
324
	if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
325
		bpp_sel = 8;
326
 
1963 serge 327
	rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
328
	if (!rfbdev)
329
		return -ENOMEM;
1126 serge 330
 
1963 serge 331
	rfbdev->rdev = rdev;
332
	rdev->mode_info.rfbdev = rfbdev;
1126 serge 333
 
5078 serge 334
	drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
335
			      &radeon_fb_helper_funcs);
336
 
1963 serge 337
	ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
338
				 rdev->num_crtc,
339
				 RADEONFB_CONN_LIMIT);
340
	if (ret) {
341
		kfree(rfbdev);
342
		return ret;
1126 serge 343
	}
344
 
1963 serge 345
	drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
1179 serge 346
 
5078 serge 347
	/* disable all the possible outputs/crtcs before entering KMS mode */
348
	drm_helper_disable_unused_functions(rdev->ddev);
1986 serge 349
 
5078 serge 350
	drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
351
LEAVE();
1963 serge 352
 
1126 serge 353
	return 0;
354
}
1963 serge 355
 
356
void radeon_fbdev_fini(struct radeon_device *rdev)
357
{
358
	if (!rdev->mode_info.rfbdev)
359
		return;
360
 
361
	radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
362
	kfree(rdev->mode_info.rfbdev);
363
	rdev->mode_info.rfbdev = NULL;
364
}
365
 
366
 
367
int radeon_fbdev_total_size(struct radeon_device *rdev)
368
{
369
	struct radeon_bo *robj;
370
	int size = 0;
371
 
1986 serge 372
	robj = gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj);
1963 serge 373
	size += radeon_bo_size(robj);
374
	return size;
375
}
376
 
377
bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
378
{
1986 serge 379
	if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
1963 serge 380
		return true;
381
	return false;
382
}