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.
46 struct drm_fb_helper helper;
47 struct amdgpu_framebuffer rfb;
48 struct amdgpu_device *adev;
52 amdgpufb_open(struct fb_info *info, int user)
54 struct amdgpu_fbdev *rfbdev = info->par;
55 struct amdgpu_device *adev = rfbdev->adev;
56 int ret = pm_runtime_get_sync(adev->ddev->dev);
57 if (ret < 0 && ret != -EACCES) {
58 pm_runtime_mark_last_busy(adev->ddev->dev);
59 pm_runtime_put_autosuspend(adev->ddev->dev);
66 amdgpufb_release(struct fb_info *info, int user)
68 struct amdgpu_fbdev *rfbdev = info->par;
69 struct amdgpu_device *adev = rfbdev->adev;
71 pm_runtime_mark_last_busy(adev->ddev->dev);
72 pm_runtime_put_autosuspend(adev->ddev->dev);
76 static struct fb_ops amdgpufb_ops = {
78 DRM_FB_HELPER_DEFAULT_OPS,
79 .fb_open = amdgpufb_open,
80 .fb_release = amdgpufb_release,
81 .fb_fillrect = drm_fb_helper_cfb_fillrect,
82 .fb_copyarea = drm_fb_helper_cfb_copyarea,
83 .fb_imageblit = drm_fb_helper_cfb_imageblit,
87 int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int cpp, bool tiled)
105 aligned += pitch_mask;
106 aligned &= ~pitch_mask;
107 return aligned * cpp;
110 static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
112 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
115 ret = amdgpu_bo_reserve(abo, true);
116 if (likely(ret == 0)) {
117 amdgpu_bo_kunmap(abo);
118 amdgpu_bo_unpin(abo);
119 amdgpu_bo_unreserve(abo);
121 drm_gem_object_put_unlocked(gobj);
124 static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
125 struct drm_mode_fb_cmd2 *mode_cmd,
126 struct drm_gem_object **gobj_p)
128 struct amdgpu_device *adev = rfbdev->adev;
129 struct drm_gem_object *gobj = NULL;
130 struct amdgpu_bo *abo = NULL;
131 bool fb_tiled = false; /* useful for testing */
132 u32 tiling_flags = 0;
134 int aligned_size, size;
135 int height = mode_cmd->height;
138 cpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0);
140 /* need to align pitch with crtc limits */
141 mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, cpp,
144 height = ALIGN(mode_cmd->height, 8);
145 size = mode_cmd->pitches[0] * height;
146 aligned_size = ALIGN(size, PAGE_SIZE);
147 ret = amdgpu_gem_object_create(adev, aligned_size, 0,
148 AMDGPU_GEM_DOMAIN_VRAM,
149 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
150 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
151 AMDGPU_GEM_CREATE_VRAM_CLEARED,
154 pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
157 abo = gem_to_amdgpu_bo(gobj);
160 tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
162 ret = amdgpu_bo_reserve(abo, false);
163 if (unlikely(ret != 0))
167 ret = amdgpu_bo_set_tiling_flags(abo,
170 dev_err(adev->dev, "FB failed to set tiling flags\n");
174 ret = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM, NULL);
176 amdgpu_bo_unreserve(abo);
179 ret = amdgpu_bo_kmap(abo, NULL);
180 amdgpu_bo_unreserve(abo);
188 amdgpufb_destroy_pinned_object(gobj);
193 static int amdgpufb_create(struct drm_fb_helper *helper,
194 struct drm_fb_helper_surface_size *sizes)
196 struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper;
197 struct amdgpu_device *adev = rfbdev->adev;
198 struct fb_info *info;
199 struct drm_framebuffer *fb = NULL;
200 struct drm_mode_fb_cmd2 mode_cmd;
201 struct drm_gem_object *gobj = NULL;
202 struct amdgpu_bo *abo = NULL;
206 mode_cmd.width = sizes->surface_width;
207 mode_cmd.height = sizes->surface_height;
209 if (sizes->surface_bpp == 24)
210 sizes->surface_bpp = 32;
212 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
213 sizes->surface_depth);
215 ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
217 DRM_ERROR("failed to create fbcon object %d\n", ret);
221 abo = gem_to_amdgpu_bo(gobj);
223 /* okay we have an object now allocate the framebuffer */
224 info = drm_fb_helper_alloc_fbi(helper);
231 info->skip_vt_switch = true;
233 ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
235 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
239 fb = &rfbdev->rfb.base;
242 rfbdev->helper.fb = fb;
244 strcpy(info->fix.id, "amdgpudrmfb");
246 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
248 info->fbops = &amdgpufb_ops;
250 tmp = amdgpu_bo_gpu_offset(abo) - adev->mc.vram_start;
251 info->fix.smem_start = adev->mc.aper_base + tmp;
252 info->fix.smem_len = amdgpu_bo_size(abo);
253 info->screen_base = amdgpu_bo_kptr(abo);
254 info->screen_size = amdgpu_bo_size(abo);
256 drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
258 /* setup aperture base/size for vesafb takeover */
259 info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
260 info->apertures->ranges[0].size = adev->mc.aper_size;
262 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
264 if (info->screen_base == NULL) {
269 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
270 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)adev->mc.aper_base);
271 DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(abo));
272 DRM_INFO("fb depth is %d\n", fb->format->depth);
273 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
275 vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
283 drm_gem_object_put_unlocked(gobj);
284 drm_framebuffer_unregister_private(fb);
285 drm_framebuffer_cleanup(fb);
291 void amdgpu_fb_output_poll_changed(struct amdgpu_device *adev)
293 if (adev->mode_info.rfbdev)
294 drm_fb_helper_hotplug_event(&adev->mode_info.rfbdev->helper);
297 static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
299 struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
301 drm_fb_helper_unregister_fbi(&rfbdev->helper);
304 amdgpufb_destroy_pinned_object(rfb->obj);
306 drm_framebuffer_unregister_private(&rfb->base);
307 drm_framebuffer_cleanup(&rfb->base);
309 drm_fb_helper_fini(&rfbdev->helper);
314 static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
315 .fb_probe = amdgpufb_create,
318 int amdgpu_fbdev_init(struct amdgpu_device *adev)
320 struct amdgpu_fbdev *rfbdev;
324 /* don't init fbdev on hw without DCE */
325 if (!adev->mode_info.mode_config_initialized)
328 /* don't init fbdev if there are no connectors */
329 if (list_empty(&adev->ddev->mode_config.connector_list))
332 /* select 8 bpp console on low vram cards */
333 if (adev->mc.real_vram_size <= (32*1024*1024))
336 rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
341 adev->mode_info.rfbdev = rfbdev;
343 drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
344 &amdgpu_fb_helper_funcs);
346 ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
347 AMDGPUFB_CONN_LIMIT);
353 drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
355 /* disable all the possible outputs/crtcs before entering KMS mode */
356 drm_helper_disable_unused_functions(adev->ddev);
358 drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
362 void amdgpu_fbdev_fini(struct amdgpu_device *adev)
364 if (!adev->mode_info.rfbdev)
367 amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
368 kfree(adev->mode_info.rfbdev);
369 adev->mode_info.rfbdev = NULL;
372 void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
374 if (adev->mode_info.rfbdev)
375 drm_fb_helper_set_suspend(&adev->mode_info.rfbdev->helper,
379 int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
381 struct amdgpu_bo *robj;
384 if (!adev->mode_info.rfbdev)
387 robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj);
388 size += amdgpu_bo_size(robj);
392 bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
394 if (!adev->mode_info.rfbdev)
396 if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj))
401 void amdgpu_fbdev_restore_mode(struct amdgpu_device *adev)
403 struct amdgpu_fbdev *afbdev;
404 struct drm_fb_helper *fb_helper;
410 afbdev = adev->mode_info.rfbdev;
415 fb_helper = &afbdev->helper;
417 ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
419 DRM_DEBUG("failed to restore crtc mode\n");