blob: d44b76455e896a539d9c859914059746fe70d25b [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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/slab.h>
Alex Deucher7c1fa1d2016-08-27 12:37:22 -040028#include <linux/pm_runtime.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029
30#include <drm/drmP.h>
31#include <drm/drm_crtc.h>
32#include <drm/drm_crtc_helper.h>
33#include <drm/amdgpu_drm.h>
34#include "amdgpu.h"
Marek Olšákfbd76d52015-05-14 23:48:26 +020035#include "cikd.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040036
37#include <drm/drm_fb_helper.h>
38
39#include <linux/vga_switcheroo.h>
40
Christian König5d43be02017-10-26 18:06:23 +020041#include "amdgpu_display.h"
42
Alex Deucherd38ceaf2015-04-20 16:55:21 -040043/* object hierarchy -
44 this contains a helper + a amdgpu fb
45 the helper contains a pointer to amdgpu framebuffer baseclass.
46*/
Alex Deucherd38ceaf2015-04-20 16:55:21 -040047
Alex Deucher7c1fa1d2016-08-27 12:37:22 -040048static int
49amdgpufb_open(struct fb_info *info, int user)
50{
51 struct amdgpu_fbdev *rfbdev = info->par;
52 struct amdgpu_device *adev = rfbdev->adev;
53 int ret = pm_runtime_get_sync(adev->ddev->dev);
54 if (ret < 0 && ret != -EACCES) {
55 pm_runtime_mark_last_busy(adev->ddev->dev);
56 pm_runtime_put_autosuspend(adev->ddev->dev);
57 return ret;
58 }
59 return 0;
60}
61
62static int
63amdgpufb_release(struct fb_info *info, int user)
64{
65 struct amdgpu_fbdev *rfbdev = info->par;
66 struct amdgpu_device *adev = rfbdev->adev;
67
68 pm_runtime_mark_last_busy(adev->ddev->dev);
69 pm_runtime_put_autosuspend(adev->ddev->dev);
70 return 0;
71}
72
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073static struct fb_ops amdgpufb_ops = {
74 .owner = THIS_MODULE,
Stefan Christea4ffff2016-11-14 00:03:13 +010075 DRM_FB_HELPER_DEFAULT_OPS,
Alex Deucher7c1fa1d2016-08-27 12:37:22 -040076 .fb_open = amdgpufb_open,
77 .fb_release = amdgpufb_release,
Archit Taneja2dbaf3922015-07-31 16:22:00 +053078 .fb_fillrect = drm_fb_helper_cfb_fillrect,
79 .fb_copyarea = drm_fb_helper_cfb_copyarea,
80 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Alex Deucherd38ceaf2015-04-20 16:55:21 -040081};
82
83
Laurent Pinchart8e911ab2016-10-18 01:41:17 +030084int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int cpp, bool tiled)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085{
86 int aligned = width;
87 int pitch_mask = 0;
88
Laurent Pinchart8e911ab2016-10-18 01:41:17 +030089 switch (cpp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 case 1:
91 pitch_mask = 255;
92 break;
93 case 2:
94 pitch_mask = 127;
95 break;
96 case 3:
97 case 4:
98 pitch_mask = 63;
99 break;
100 }
101
102 aligned += pitch_mask;
103 aligned &= ~pitch_mask;
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300104 return aligned * cpp;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105}
106
107static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
108{
Christian König765e7fb2016-09-15 15:06:50 +0200109 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110 int ret;
111
Michel Dänzerc81a1a72017-04-28 17:28:14 +0900112 ret = amdgpu_bo_reserve(abo, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113 if (likely(ret == 0)) {
Christian König765e7fb2016-09-15 15:06:50 +0200114 amdgpu_bo_kunmap(abo);
115 amdgpu_bo_unpin(abo);
116 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117 }
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300118 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119}
120
121static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
122 struct drm_mode_fb_cmd2 *mode_cmd,
123 struct drm_gem_object **gobj_p)
124{
125 struct amdgpu_device *adev = rfbdev->adev;
126 struct drm_gem_object *gobj = NULL;
Christian König765e7fb2016-09-15 15:06:50 +0200127 struct amdgpu_bo *abo = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400128 bool fb_tiled = false; /* useful for testing */
Christian König5d43be02017-10-26 18:06:23 +0200129 u32 tiling_flags = 0, domain;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130 int ret;
131 int aligned_size, size;
132 int height = mode_cmd->height;
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300133 u32 cpp;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300135 cpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400136
137 /* need to align pitch with crtc limits */
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300138 mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, cpp,
139 fb_tiled);
Samuel Li1d2361e2018-04-18 15:06:02 -0400140 domain = amdgpu_display_supported_domains(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141
142 height = ALIGN(mode_cmd->height, 8);
143 size = mode_cmd->pitches[0] * height;
144 aligned_size = ALIGN(size, PAGE_SIZE);
Christian König5d43be02017-10-26 18:06:23 +0200145 ret = amdgpu_gem_object_create(adev, aligned_size, 0, domain,
Christian König03f48dd2016-08-15 17:00:22 +0200146 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
Pixel Dingcbabc8b2017-01-24 11:39:48 +0800147 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
148 AMDGPU_GEM_CREATE_VRAM_CLEARED,
Huang Ruif8ddb392018-06-28 14:38:21 +0800149 ttm_bo_type_kernel, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150 if (ret) {
Joe Perches7ca85292017-02-28 04:55:52 -0800151 pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152 return -ENOMEM;
153 }
Christian König765e7fb2016-09-15 15:06:50 +0200154 abo = gem_to_amdgpu_bo(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155
156 if (fb_tiled)
Marek Olšákfbd76d52015-05-14 23:48:26 +0200157 tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400158
Christian König765e7fb2016-09-15 15:06:50 +0200159 ret = amdgpu_bo_reserve(abo, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160 if (unlikely(ret != 0))
161 goto out_unref;
162
163 if (tiling_flags) {
Christian König765e7fb2016-09-15 15:06:50 +0200164 ret = amdgpu_bo_set_tiling_flags(abo,
Marek Olšák63ab1c22015-05-14 23:03:57 +0200165 tiling_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400166 if (ret)
167 dev_err(adev->dev, "FB failed to set tiling flags\n");
168 }
169
170
Junwei Zhang7b7c6c82018-06-25 12:51:14 +0800171 ret = amdgpu_bo_pin(abo, domain);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172 if (ret) {
Christian König765e7fb2016-09-15 15:06:50 +0200173 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174 goto out_unref;
175 }
Junwei Zhangbb812f12018-06-25 13:32:24 +0800176
177 ret = amdgpu_ttm_alloc_gart(&abo->tbo);
178 if (ret) {
179 amdgpu_bo_unreserve(abo);
180 dev_err(adev->dev, "%p bind failed\n", abo);
181 goto out_unref;
182 }
183
Christian König765e7fb2016-09-15 15:06:50 +0200184 ret = amdgpu_bo_kmap(abo, NULL);
185 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400186 if (ret) {
187 goto out_unref;
188 }
189
190 *gobj_p = gobj;
191 return 0;
192out_unref:
193 amdgpufb_destroy_pinned_object(gobj);
194 *gobj_p = NULL;
195 return ret;
196}
197
198static int amdgpufb_create(struct drm_fb_helper *helper,
199 struct drm_fb_helper_surface_size *sizes)
200{
201 struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper;
202 struct amdgpu_device *adev = rfbdev->adev;
203 struct fb_info *info;
204 struct drm_framebuffer *fb = NULL;
205 struct drm_mode_fb_cmd2 mode_cmd;
206 struct drm_gem_object *gobj = NULL;
Christian König765e7fb2016-09-15 15:06:50 +0200207 struct amdgpu_bo *abo = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208 int ret;
209 unsigned long tmp;
210
211 mode_cmd.width = sizes->surface_width;
212 mode_cmd.height = sizes->surface_height;
213
214 if (sizes->surface_bpp == 24)
215 sizes->surface_bpp = 32;
216
217 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
218 sizes->surface_depth);
219
220 ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
221 if (ret) {
222 DRM_ERROR("failed to create fbcon object %d\n", ret);
223 return ret;
224 }
225
Christian König765e7fb2016-09-15 15:06:50 +0200226 abo = gem_to_amdgpu_bo(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400227
228 /* okay we have an object now allocate the framebuffer */
Archit Taneja2dbaf3922015-07-31 16:22:00 +0530229 info = drm_fb_helper_alloc_fbi(helper);
230 if (IS_ERR(info)) {
231 ret = PTR_ERR(info);
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100232 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400233 }
234
235 info->par = rfbdev;
Alex Deucherdf7989f2015-11-02 10:52:32 -0500236 info->skip_vt_switch = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400237
Samuel Li9da3f2d2018-01-19 12:17:42 -0500238 ret = amdgpu_display_framebuffer_init(adev->ddev, &rfbdev->rfb,
239 &mode_cmd, gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240 if (ret) {
241 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100242 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400243 }
244
245 fb = &rfbdev->rfb.base;
246
247 /* setup helper */
248 rfbdev->helper.fb = fb;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400249
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250 strcpy(info->fix.id, "amdgpudrmfb");
251
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200252 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400253
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254 info->fbops = &amdgpufb_ops;
255
Christian König770d13b2018-01-12 14:52:22 +0100256 tmp = amdgpu_bo_gpu_offset(abo) - adev->gmc.vram_start;
257 info->fix.smem_start = adev->gmc.aper_base + tmp;
Christian König765e7fb2016-09-15 15:06:50 +0200258 info->fix.smem_len = amdgpu_bo_size(abo);
Christian Königf5e1c742017-07-20 23:45:18 +0200259 info->screen_base = amdgpu_bo_kptr(abo);
Christian König765e7fb2016-09-15 15:06:50 +0200260 info->screen_size = amdgpu_bo_size(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400261
262 drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
263
264 /* setup aperture base/size for vesafb takeover */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400265 info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
Christian König770d13b2018-01-12 14:52:22 +0100266 info->apertures->ranges[0].size = adev->gmc.aper_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400267
268 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
269
270 if (info->screen_base == NULL) {
271 ret = -ENOSPC;
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100272 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400273 }
274
275 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
Christian König770d13b2018-01-12 14:52:22 +0100276 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)adev->gmc.aper_base);
Christian König765e7fb2016-09-15 15:06:50 +0200277 DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(abo));
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200278 DRM_INFO("fb depth is %d\n", fb->format->depth);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400279 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
280
281 vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
282 return 0;
283
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100284out:
Christian König765e7fb2016-09-15 15:06:50 +0200285 if (abo) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400286
287 }
288 if (fb && ret) {
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300289 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290 drm_framebuffer_unregister_private(fb);
291 drm_framebuffer_cleanup(fb);
292 kfree(fb);
293 }
294 return ret;
295}
296
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400297static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
298{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400299 struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
300
Archit Taneja2dbaf3922015-07-31 16:22:00 +0530301 drm_fb_helper_unregister_fbi(&rfbdev->helper);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400302
Daniel Stonee68d14d2018-03-30 15:11:38 +0100303 if (rfb->base.obj[0]) {
304 amdgpufb_destroy_pinned_object(rfb->base.obj[0]);
305 rfb->base.obj[0] = NULL;
Michel Dänzera072c5f2017-09-11 17:04:41 +0900306 drm_framebuffer_unregister_private(&rfb->base);
307 drm_framebuffer_cleanup(&rfb->base);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400308 }
309 drm_fb_helper_fini(&rfbdev->helper);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400310
311 return 0;
312}
313
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400314static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400315 .fb_probe = amdgpufb_create,
316};
317
318int amdgpu_fbdev_init(struct amdgpu_device *adev)
319{
320 struct amdgpu_fbdev *rfbdev;
321 int bpp_sel = 32;
322 int ret;
323
324 /* don't init fbdev on hw without DCE */
325 if (!adev->mode_info.mode_config_initialized)
326 return 0;
327
Alex Deucherf49d45c2016-01-26 00:30:33 -0500328 /* don't init fbdev if there are no connectors */
329 if (list_empty(&adev->ddev->mode_config.connector_list))
330 return 0;
331
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400332 /* select 8 bpp console on low vram cards */
Christian König770d13b2018-01-12 14:52:22 +0100333 if (adev->gmc.real_vram_size <= (32*1024*1024))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400334 bpp_sel = 8;
335
336 rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
337 if (!rfbdev)
338 return -ENOMEM;
339
340 rfbdev->adev = adev;
341 adev->mode_info.rfbdev = rfbdev;
342
343 drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
344 &amdgpu_fb_helper_funcs);
345
346 ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347 AMDGPUFB_CONN_LIMIT);
348 if (ret) {
349 kfree(rfbdev);
350 return ret;
351 }
352
353 drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
354
355 /* disable all the possible outputs/crtcs before entering KMS mode */
Andrey Grodzovsky93b8ca92017-05-30 16:49:59 -0400356 if (!amdgpu_device_has_dc_support(adev))
357 drm_helper_disable_unused_functions(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400358
359 drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
360 return 0;
361}
362
363void amdgpu_fbdev_fini(struct amdgpu_device *adev)
364{
365 if (!adev->mode_info.rfbdev)
366 return;
367
368 amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
369 kfree(adev->mode_info.rfbdev);
370 adev->mode_info.rfbdev = NULL;
371}
372
373void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
374{
375 if (adev->mode_info.rfbdev)
Archit Taneja2dbaf3922015-07-31 16:22:00 +0530376 drm_fb_helper_set_suspend(&adev->mode_info.rfbdev->helper,
377 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400378}
379
380int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
381{
382 struct amdgpu_bo *robj;
383 int size = 0;
384
385 if (!adev->mode_info.rfbdev)
386 return 0;
387
Daniel Stonee68d14d2018-03-30 15:11:38 +0100388 robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.base.obj[0]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400389 size += amdgpu_bo_size(robj);
390 return size;
391}
392
393bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
394{
395 if (!adev->mode_info.rfbdev)
396 return false;
Daniel Stonee68d14d2018-03-30 15:11:38 +0100397 if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.base.obj[0]))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400398 return true;
399 return false;
400}