2 * Copyright © 2007 David Airlie
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:
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
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.
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/pm_runtime.h>
31 #include <drm/drm_crtc.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/amdgpu_drm.h>
37 #include <drm/drm_fb_helper.h>
39 #include <linux/vga_switcheroo.h>
42 this contains a helper + a amdgpu fb
43 the helper contains a pointer to amdgpu framebuffer baseclass.
47 amdgpufb_open(struct fb_info *info, int user)
49 struct amdgpu_fbdev *rfbdev = info->par;
50 struct amdgpu_device *adev = rfbdev->adev;
51 int ret = pm_runtime_get_sync(adev->ddev->dev);
52 if (ret < 0 && ret != -EACCES) {
53 pm_runtime_mark_last_busy(adev->ddev->dev);
54 pm_runtime_put_autosuspend(adev->ddev->dev);
61 amdgpufb_release(struct fb_info *info, int user)
63 struct amdgpu_fbdev *rfbdev = info->par;
64 struct amdgpu_device *adev = rfbdev->adev;
66 pm_runtime_mark_last_busy(adev->ddev->dev);
67 pm_runtime_put_autosuspend(adev->ddev->dev);
71 static struct fb_ops amdgpufb_ops = {
73 DRM_FB_HELPER_DEFAULT_OPS,
74 .fb_open = amdgpufb_open,
75 .fb_release = amdgpufb_release,
76 .fb_fillrect = drm_fb_helper_cfb_fillrect,
77 .fb_copyarea = drm_fb_helper_cfb_copyarea,
78 .fb_imageblit = drm_fb_helper_cfb_imageblit,
82 int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int cpp, bool tiled)
100 aligned += pitch_mask;
101 aligned &= ~pitch_mask;
102 return aligned * cpp;
105 static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
107 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
110 ret = amdgpu_bo_reserve(abo, true);
111 if (likely(ret == 0)) {
112 amdgpu_bo_kunmap(abo);
113 amdgpu_bo_unpin(abo);
114 amdgpu_bo_unreserve(abo);
116 drm_gem_object_put_unlocked(gobj);
119 static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
120 struct drm_mode_fb_cmd2 *mode_cmd,
121 struct drm_gem_object **gobj_p)
123 struct amdgpu_device *adev = rfbdev->adev;
124 struct drm_gem_object *gobj = NULL;
125 struct amdgpu_bo *abo = NULL;
126 bool fb_tiled = false; /* useful for testing */
127 u32 tiling_flags = 0;
129 int aligned_size, size;
130 int height = mode_cmd->height;
133 cpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0);
135 /* need to align pitch with crtc limits */
136 mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, cpp,
139 height = ALIGN(mode_cmd->height, 8);
140 size = mode_cmd->pitches[0] * height;
141 aligned_size = ALIGN(size, PAGE_SIZE);
142 ret = amdgpu_gem_object_create(adev, aligned_size, 0,
143 AMDGPU_GEM_DOMAIN_VRAM,
144 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
145 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
146 AMDGPU_GEM_CREATE_VRAM_CLEARED,
149 pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
152 abo = gem_to_amdgpu_bo(gobj);
155 tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
157 ret = amdgpu_bo_reserve(abo, false);
158 if (unlikely(ret != 0))
162 ret = amdgpu_bo_set_tiling_flags(abo,
165 dev_err(adev->dev, "FB failed to set tiling flags\n");
169 ret = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM, NULL);
171 amdgpu_bo_unreserve(abo);
174 ret = amdgpu_bo_kmap(abo, NULL);
175 amdgpu_bo_unreserve(abo);
183 amdgpufb_destroy_pinned_object(gobj);
188 static int amdgpufb_create(struct drm_fb_helper *helper,
189 struct drm_fb_helper_surface_size *sizes)
191 struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper;
192 struct amdgpu_device *adev = rfbdev->adev;
193 struct fb_info *info;
194 struct drm_framebuffer *fb = NULL;
195 struct drm_mode_fb_cmd2 mode_cmd;
196 struct drm_gem_object *gobj = NULL;
197 struct amdgpu_bo *abo = NULL;
201 mode_cmd.width = sizes->surface_width;
202 mode_cmd.height = sizes->surface_height;
204 if (sizes->surface_bpp == 24)
205 sizes->surface_bpp = 32;
207 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
208 sizes->surface_depth);
210 ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
212 DRM_ERROR("failed to create fbcon object %d\n", ret);
216 abo = gem_to_amdgpu_bo(gobj);
218 /* okay we have an object now allocate the framebuffer */
219 info = drm_fb_helper_alloc_fbi(helper);
226 info->skip_vt_switch = true;
228 ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
230 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
234 fb = &rfbdev->rfb.base;
237 rfbdev->helper.fb = fb;
239 strcpy(info->fix.id, "amdgpudrmfb");
241 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
243 info->fbops = &amdgpufb_ops;
245 tmp = amdgpu_bo_gpu_offset(abo) - adev->mc.vram_start;
246 info->fix.smem_start = adev->mc.aper_base + tmp;
247 info->fix.smem_len = amdgpu_bo_size(abo);
248 info->screen_base = amdgpu_bo_kptr(abo);
249 info->screen_size = amdgpu_bo_size(abo);
251 drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
253 /* setup aperture base/size for vesafb takeover */
254 info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
255 info->apertures->ranges[0].size = adev->mc.aper_size;
257 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
259 if (info->screen_base == NULL) {
264 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
265 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)adev->mc.aper_base);
266 DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(abo));
267 DRM_INFO("fb depth is %d\n", fb->format->depth);
268 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
270 vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
278 drm_gem_object_put_unlocked(gobj);
279 drm_framebuffer_unregister_private(fb);
280 drm_framebuffer_cleanup(fb);
286 void amdgpu_fb_output_poll_changed(struct amdgpu_device *adev)
288 if (adev->mode_info.rfbdev)
289 drm_fb_helper_hotplug_event(&adev->mode_info.rfbdev->helper);
292 static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
294 struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
296 drm_fb_helper_unregister_fbi(&rfbdev->helper);
299 amdgpufb_destroy_pinned_object(rfb->obj);
301 drm_framebuffer_unregister_private(&rfb->base);
302 drm_framebuffer_cleanup(&rfb->base);
304 drm_fb_helper_fini(&rfbdev->helper);
309 static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
310 .fb_probe = amdgpufb_create,
313 int amdgpu_fbdev_init(struct amdgpu_device *adev)
315 struct amdgpu_fbdev *rfbdev;
319 /* don't init fbdev on hw without DCE */
320 if (!adev->mode_info.mode_config_initialized)
323 /* don't init fbdev if there are no connectors */
324 if (list_empty(&adev->ddev->mode_config.connector_list))
327 /* select 8 bpp console on low vram cards */
328 if (adev->mc.real_vram_size <= (32*1024*1024))
331 rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
336 adev->mode_info.rfbdev = rfbdev;
338 drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
339 &amdgpu_fb_helper_funcs);
341 ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
342 AMDGPUFB_CONN_LIMIT);
348 drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
350 /* disable all the possible outputs/crtcs before entering KMS mode */
351 if (!amdgpu_device_has_dc_support(adev))
352 drm_helper_disable_unused_functions(adev->ddev);
354 drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
358 void amdgpu_fbdev_fini(struct amdgpu_device *adev)
360 if (!adev->mode_info.rfbdev)
363 amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
364 kfree(adev->mode_info.rfbdev);
365 adev->mode_info.rfbdev = NULL;
368 void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
370 if (adev->mode_info.rfbdev)
371 drm_fb_helper_set_suspend(&adev->mode_info.rfbdev->helper,
375 int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
377 struct amdgpu_bo *robj;
380 if (!adev->mode_info.rfbdev)
383 robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj);
384 size += amdgpu_bo_size(robj);
388 bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
390 if (!adev->mode_info.rfbdev)
392 if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj))
397 void amdgpu_fbdev_restore_mode(struct amdgpu_device *adev)
399 struct amdgpu_fbdev *afbdev;
400 struct drm_fb_helper *fb_helper;
406 afbdev = adev->mode_info.rfbdev;
411 fb_helper = &afbdev->helper;
413 ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
415 DRM_DEBUG("failed to restore crtc mode\n");