xref: /dragonfly/sys/dev/drm/radeon/radeon_fb.c (revision 8af44722)
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  */
26 #include <linux/module.h>
27 #include <linux/fb.h>
28 #include <linux/pm_runtime.h>
29 
30 #include <drm/drmP.h>
31 #include <drm/drm_crtc.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/radeon_drm.h>
34 #include "radeon.h"
35 
36 #include <drm/drm_fb_helper.h>
37 
38 #include <linux/vga_switcheroo.h>
39 
40 /* object hierarchy -
41  * this contains a helper + a radeon fb
42  * the helper contains a pointer to radeon framebuffer baseclass.
43  */
44 struct radeon_fbdev {
45 	struct drm_fb_helper helper;
46 	struct radeon_framebuffer rfb;
47 	struct radeon_device *rdev;
48 };
49 
50 #if 0
51 static int
52 radeonfb_open(struct fb_info *info, int user)
53 {
54 	struct radeon_fbdev *rfbdev = info->par;
55 	struct radeon_device *rdev = rfbdev->rdev;
56 	int ret = pm_runtime_get_sync(rdev->ddev->dev);
57 	if (ret < 0 && ret != -EACCES) {
58 		pm_runtime_mark_last_busy(rdev->ddev->dev);
59 		pm_runtime_put_autosuspend(rdev->ddev->dev);
60 		return ret;
61 	}
62 	return 0;
63 }
64 
65 static int
66 radeonfb_release(struct fb_info *info, int user)
67 {
68 	struct radeon_fbdev *rfbdev = info->par;
69 	struct radeon_device *rdev = rfbdev->rdev;
70 
71 	pm_runtime_mark_last_busy(rdev->ddev->dev);
72 	pm_runtime_put_autosuspend(rdev->ddev->dev);
73 	return 0;
74 }
75 #endif
76 
77 static struct fb_ops radeonfb_ops = {
78 #if 0
79 	.owner = THIS_MODULE,
80 	.fb_open = radeonfb_open,
81 	.fb_release = radeonfb_release,
82 	.fb_check_var = drm_fb_helper_check_var,
83 #endif
84 	.fb_set_par = drm_fb_helper_set_par,
85 #if 0
86 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
87 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
88 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
89 	.fb_pan_display = drm_fb_helper_pan_display,
90 #endif
91 	.fb_blank = drm_fb_helper_blank,
92 #if 0
93 	.fb_setcmap = drm_fb_helper_setcmap,
94 #endif
95 	.fb_debug_enter = drm_fb_helper_debug_enter,
96 #if 0
97 	.fb_debug_leave = drm_fb_helper_debug_leave,
98 #endif
99 };
100 
101 
102 int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
103 {
104 	int aligned = width;
105 	int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
106 	int pitch_mask = 0;
107 
108 	switch (bpp / 8) {
109 	case 1:
110 		pitch_mask = align_large ? 255 : 127;
111 		break;
112 	case 2:
113 		pitch_mask = align_large ? 127 : 31;
114 		break;
115 	case 3:
116 	case 4:
117 		pitch_mask = align_large ? 63 : 15;
118 		break;
119 	}
120 
121 	aligned += pitch_mask;
122 	aligned &= ~pitch_mask;
123 	return aligned;
124 }
125 
126 static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
127 {
128 	struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
129 	int ret;
130 
131 	ret = radeon_bo_reserve(rbo, false);
132 	if (likely(ret == 0)) {
133 		radeon_bo_kunmap(rbo);
134 		radeon_bo_unpin(rbo);
135 		radeon_bo_unreserve(rbo);
136 	}
137 	drm_gem_object_unreference_unlocked(gobj);
138 }
139 
140 static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
141 					 struct drm_mode_fb_cmd2 *mode_cmd,
142 					 struct drm_gem_object **gobj_p)
143 {
144 	struct radeon_device *rdev = rfbdev->rdev;
145 	struct drm_gem_object *gobj = NULL;
146 	struct radeon_bo *rbo = NULL;
147 	bool fb_tiled = false; /* useful for testing */
148 	u32 tiling_flags = 0;
149 	int ret;
150 	int aligned_size, size;
151 	int height = mode_cmd->height;
152 	u32 bpp, depth;
153 
154 	drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
155 
156 	/* need to align pitch with crtc limits */
157 	mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, bpp,
158 						  fb_tiled) * ((bpp + 1) / 8);
159 
160 	if (rdev->family >= CHIP_R600)
161 		height = ALIGN(mode_cmd->height, 8);
162 	size = mode_cmd->pitches[0] * height;
163 	aligned_size = ALIGN(size, PAGE_SIZE);
164 	ret = radeon_gem_object_create(rdev, aligned_size, 0,
165 				       RADEON_GEM_DOMAIN_VRAM,
166 				       0, true, &gobj);
167 	if (ret) {
168 		printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
169 		       aligned_size);
170 		return -ENOMEM;
171 	}
172 	rbo = gem_to_radeon_bo(gobj);
173 
174 	if (fb_tiled)
175 		tiling_flags = RADEON_TILING_MACRO;
176 
177 #ifdef __BIG_ENDIAN
178 	switch (bpp) {
179 	case 32:
180 		tiling_flags |= RADEON_TILING_SWAP_32BIT;
181 		break;
182 	case 16:
183 		tiling_flags |= RADEON_TILING_SWAP_16BIT;
184 	default:
185 		break;
186 	}
187 #endif
188 
189 	if (tiling_flags) {
190 		ret = radeon_bo_set_tiling_flags(rbo,
191 						 tiling_flags | RADEON_TILING_SURFACE,
192 						 mode_cmd->pitches[0]);
193 		if (ret)
194 			dev_err(rdev->dev, "FB failed to set tiling flags\n");
195 	}
196 
197 
198 	ret = radeon_bo_reserve(rbo, false);
199 	if (unlikely(ret != 0))
200 		goto out_unref;
201 	/* Only 27 bit offset for legacy CRTC */
202 	ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
203 				       ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
204 				       NULL);
205 	if (ret) {
206 		radeon_bo_unreserve(rbo);
207 		goto out_unref;
208 	}
209 	if (fb_tiled)
210 		radeon_bo_check_tiling(rbo, 0, 0);
211 	ret = radeon_bo_kmap(rbo, NULL);
212 	radeon_bo_unreserve(rbo);
213 	if (ret) {
214 		goto out_unref;
215 	}
216 
217 	*gobj_p = gobj;
218 	return 0;
219 out_unref:
220 	radeonfb_destroy_pinned_object(gobj);
221 	*gobj_p = NULL;
222 	return ret;
223 }
224 
225 static int radeonfb_create(struct drm_fb_helper *helper,
226 			   struct drm_fb_helper_surface_size *sizes)
227 {
228 	struct radeon_fbdev *rfbdev =
229 		container_of(helper, struct radeon_fbdev, helper);
230 	struct radeon_device *rdev = rfbdev->rdev;
231 	struct fb_info *info;
232 	struct drm_framebuffer *fb = NULL;
233 	struct drm_mode_fb_cmd2 mode_cmd;
234 	struct drm_gem_object *gobj = NULL;
235 	struct radeon_bo *rbo = NULL;
236 	int ret;
237 	device_t vga_dev = device_get_parent(rdev->dev->bsddev);
238 
239 	mode_cmd.width = sizes->surface_width;
240 	mode_cmd.height = sizes->surface_height;
241 
242 	/* avivo can't scanout real 24bpp */
243 	if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
244 		sizes->surface_bpp = 32;
245 
246 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
247 							  sizes->surface_depth);
248 
249 	ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
250 	if (ret) {
251 		DRM_ERROR("failed to create fbcon object %d\n", ret);
252 		return ret;
253 	}
254 
255 	rbo = gem_to_radeon_bo(gobj);
256 
257 	/* okay we have an object now allocate the framebuffer */
258 	info = drm_fb_helper_alloc_fbi(helper);
259 	if (IS_ERR(info)) {
260 		ret = PTR_ERR(info);
261 		goto out_unref;
262 	}
263 
264 	info->par = rfbdev;
265 
266 	ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
267 	if (ret) {
268 		DRM_ERROR("failed to initialize framebuffer %d\n", ret);
269 		goto out_destroy_fbi;
270 	}
271 
272 	fb = &rfbdev->rfb.base;
273 
274 	/* setup helper */
275 	rfbdev->helper.fb = fb;
276 
277 	memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
278 
279 #ifdef __DragonFly__
280 	info->width = sizes->fb_width;
281 	info->height = sizes->fb_height;
282 	info->stride = fb->pitches[0];
283 	info->depth = sizes->surface_bpp;
284 	info->is_vga_boot_display = vga_pci_is_boot_display(vga_dev);
285 	info->fbops = radeonfb_ops;
286 	/* We use info->screen_base and info->screen_size would-be values */
287 #if 0
288 	info->paddr = (vm_paddr_t)rbo->kptr;
289 	info->vaddr = (vm_offset_t)pmap_mapdev_attr(info->paddr,
290 		radeon_bo_size(rbo),
291 		VM_MEMATTR_WRITE_COMBINING);
292 #endif
293 	unsigned long tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
294 	info->vaddr = (vm_offset_t)rbo->kptr;
295 	info->paddr = rdev->mc.aper_base + tmp;
296 #else
297 	strcpy(info->fix.id, "radeondrmfb");
298 
299 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
300 
301 	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
302 	info->fbops = &radeonfb_ops;
303 
304 	tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
305 	info->fix.smem_start = rdev->mc.aper_base + tmp;
306 	info->fix.smem_len = radeon_bo_size(rbo);
307 	info->screen_base = rbo->kptr;
308 	info->screen_size = radeon_bo_size(rbo);
309 
310 	drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
311 
312 	/* setup aperture base/size for vesafb takeover */
313 	info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
314 	info->apertures->ranges[0].size = rdev->mc.aper_size;
315 
316 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
317 
318 	if (info->screen_base == NULL) {
319 		ret = -ENOSPC;
320 		goto out_destroy_fbi;
321 	}
322 #endif
323 
324 	DRM_INFO("fb mappable at 0x%jX\n",  info->paddr);
325 	DRM_INFO("vram apper at 0x%lX\n",  (unsigned long)rdev->mc.aper_base);
326 	DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
327 	DRM_INFO("fb depth is %d\n", fb->depth);
328 	DRM_INFO("   pitch is %d\n", fb->pitches[0]);
329 
330 	vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
331 	return 0;
332 
333 out_destroy_fbi:
334 	drm_fb_helper_release_fbi(helper);
335 out_unref:
336 	if (rbo) {
337 
338 	}
339 	if (fb && ret) {
340 		drm_gem_object_unreference_unlocked(gobj);
341 		drm_framebuffer_unregister_private(fb);
342 		drm_framebuffer_cleanup(fb);
343 		kfree(fb);
344 	}
345 	return ret;
346 }
347 
348 void radeon_fb_output_poll_changed(struct radeon_device *rdev)
349 {
350 	if (rdev->mode_info.rfbdev)
351 		drm_fb_helper_hotplug_event(&rdev->mode_info.rfbdev->helper);
352 }
353 
354 static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
355 {
356 	struct radeon_framebuffer *rfb = &rfbdev->rfb;
357 
358 	drm_fb_helper_unregister_fbi(&rfbdev->helper);
359 	drm_fb_helper_release_fbi(&rfbdev->helper);
360 
361 	if (rfb->obj) {
362 		radeonfb_destroy_pinned_object(rfb->obj);
363 		rfb->obj = NULL;
364 	}
365 	drm_fb_helper_fini(&rfbdev->helper);
366 	drm_framebuffer_unregister_private(&rfb->base);
367 	drm_framebuffer_cleanup(&rfb->base);
368 
369 	return 0;
370 }
371 
372 static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
373 	.gamma_set = radeon_crtc_fb_gamma_set,
374 	.gamma_get = radeon_crtc_fb_gamma_get,
375 	.fb_probe = radeonfb_create,
376 };
377 
378 int radeon_fbdev_init(struct radeon_device *rdev)
379 {
380 	struct radeon_fbdev *rfbdev;
381 	int bpp_sel = 32;
382 	int ret;
383 
384 	/* don't enable fbdev if no connectors */
385 	if (list_empty(&rdev->ddev->mode_config.connector_list))
386 		return 0;
387 
388 	/* select 8 bpp console on RN50 or 16MB cards */
389 	if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
390 		bpp_sel = 8;
391 
392 	rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
393 	if (!rfbdev)
394 		return -ENOMEM;
395 
396 	rfbdev->rdev = rdev;
397 	rdev->mode_info.rfbdev = rfbdev;
398 
399 	drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
400 			      &radeon_fb_helper_funcs);
401 
402 	ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
403 				 rdev->num_crtc,
404 				 RADEONFB_CONN_LIMIT);
405 	if (ret)
406 		goto free;
407 
408 	ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
409 	if (ret)
410 		goto fini;
411 
412 	/* disable all the possible outputs/crtcs before entering KMS mode */
413 	drm_helper_disable_unused_functions(rdev->ddev);
414 
415 	ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
416 	if (ret)
417 		goto fini;
418 
419 	return 0;
420 
421 fini:
422 	drm_fb_helper_fini(&rfbdev->helper);
423 free:
424 	kfree(rfbdev);
425 	return ret;
426 }
427 
428 void radeon_fbdev_fini(struct radeon_device *rdev)
429 {
430 	if (!rdev->mode_info.rfbdev)
431 		return;
432 
433 	radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
434 	kfree(rdev->mode_info.rfbdev);
435 	rdev->mode_info.rfbdev = NULL;
436 }
437 
438 void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
439 {
440 #ifdef DUMBBELL_WIP
441 	if (rdev->mode_info.rfbdev)
442 		drm_fb_helper_set_suspend(&rdev->mode_info.rfbdev->helper, state);
443 #endif /* DUMBBELL_WIP */
444 }
445 
446 bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
447 {
448 	if (!rdev->mode_info.rfbdev)
449 		return false;
450 
451 	if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
452 		return true;
453 	return false;
454 }
455 
456 void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
457 {
458 	if (rdev->mode_info.rfbdev)
459 		drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
460 }
461 
462 void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
463 {
464 	if (rdev->mode_info.rfbdev)
465 		drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
466 }
467 
468 void radeon_fbdev_restore_mode(struct radeon_device *rdev)
469 {
470 	struct radeon_fbdev *rfbdev = rdev->mode_info.rfbdev;
471 	struct drm_fb_helper *fb_helper;
472 	int ret;
473 
474 	if (!rfbdev)
475 		return;
476 
477 	fb_helper = &rfbdev->helper;
478 
479 	ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
480 	if (ret)
481 		DRM_DEBUG("failed to restore crtc mode\n");
482 }
483