blob: ff3e9beb7d19c64c3a7161953a844cebb9f5bb8d [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
41/* object hierarchy -
42 this contains a helper + a amdgpu fb
43 the helper contains a pointer to amdgpu framebuffer baseclass.
44*/
Alex Deucherd38ceaf2015-04-20 16:55:21 -040045
Alex Deucher7c1fa1d2016-08-27 12:37:22 -040046static int
47amdgpufb_open(struct fb_info *info, int user)
48{
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);
55 return ret;
56 }
57 return 0;
58}
59
60static int
61amdgpufb_release(struct fb_info *info, int user)
62{
63 struct amdgpu_fbdev *rfbdev = info->par;
64 struct amdgpu_device *adev = rfbdev->adev;
65
66 pm_runtime_mark_last_busy(adev->ddev->dev);
67 pm_runtime_put_autosuspend(adev->ddev->dev);
68 return 0;
69}
70
Alex Deucherd38ceaf2015-04-20 16:55:21 -040071static struct fb_ops amdgpufb_ops = {
72 .owner = THIS_MODULE,
Stefan Christea4ffff2016-11-14 00:03:13 +010073 DRM_FB_HELPER_DEFAULT_OPS,
Alex Deucher7c1fa1d2016-08-27 12:37:22 -040074 .fb_open = amdgpufb_open,
75 .fb_release = amdgpufb_release,
Archit Taneja2dbaf3922015-07-31 16:22:00 +053076 .fb_fillrect = drm_fb_helper_cfb_fillrect,
77 .fb_copyarea = drm_fb_helper_cfb_copyarea,
78 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Alex Deucherd38ceaf2015-04-20 16:55:21 -040079};
80
81
Laurent Pinchart8e911ab2016-10-18 01:41:17 +030082int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int cpp, bool tiled)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083{
84 int aligned = width;
85 int pitch_mask = 0;
86
Laurent Pinchart8e911ab2016-10-18 01:41:17 +030087 switch (cpp) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040088 case 1:
89 pitch_mask = 255;
90 break;
91 case 2:
92 pitch_mask = 127;
93 break;
94 case 3:
95 case 4:
96 pitch_mask = 63;
97 break;
98 }
99
100 aligned += pitch_mask;
101 aligned &= ~pitch_mask;
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300102 return aligned * cpp;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103}
104
105static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
106{
Christian König765e7fb2016-09-15 15:06:50 +0200107 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108 int ret;
109
Michel Dänzerc81a1a72017-04-28 17:28:14 +0900110 ret = amdgpu_bo_reserve(abo, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400111 if (likely(ret == 0)) {
Christian König765e7fb2016-09-15 15:06:50 +0200112 amdgpu_bo_kunmap(abo);
113 amdgpu_bo_unpin(abo);
114 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115 }
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300116 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117}
118
119static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
120 struct drm_mode_fb_cmd2 *mode_cmd,
121 struct drm_gem_object **gobj_p)
122{
123 struct amdgpu_device *adev = rfbdev->adev;
124 struct drm_gem_object *gobj = NULL;
Christian König765e7fb2016-09-15 15:06:50 +0200125 struct amdgpu_bo *abo = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126 bool fb_tiled = false; /* useful for testing */
127 u32 tiling_flags = 0;
128 int ret;
129 int aligned_size, size;
130 int height = mode_cmd->height;
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300131 u32 cpp;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300133 cpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134
135 /* need to align pitch with crtc limits */
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300136 mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, cpp,
137 fb_tiled);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138
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,
Christian König03f48dd2016-08-15 17:00:22 +0200144 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
Pixel Dingcbabc8b2017-01-24 11:39:48 +0800145 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
146 AMDGPU_GEM_CREATE_VRAM_CLEARED,
Christian Könige1eb899b42017-08-25 09:14:43 +0200147 true, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400148 if (ret) {
Joe Perches7ca85292017-02-28 04:55:52 -0800149 pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150 return -ENOMEM;
151 }
Christian König765e7fb2016-09-15 15:06:50 +0200152 abo = gem_to_amdgpu_bo(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400153
154 if (fb_tiled)
Marek Olšákfbd76d52015-05-14 23:48:26 +0200155 tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156
Christian König765e7fb2016-09-15 15:06:50 +0200157 ret = amdgpu_bo_reserve(abo, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400158 if (unlikely(ret != 0))
159 goto out_unref;
160
161 if (tiling_flags) {
Christian König765e7fb2016-09-15 15:06:50 +0200162 ret = amdgpu_bo_set_tiling_flags(abo,
Marek Olšák63ab1c22015-05-14 23:03:57 +0200163 tiling_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164 if (ret)
165 dev_err(adev->dev, "FB failed to set tiling flags\n");
166 }
167
168
Alex Deucher7fe28572016-12-07 16:14:38 -0500169 ret = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400170 if (ret) {
Christian König765e7fb2016-09-15 15:06:50 +0200171 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172 goto out_unref;
173 }
Christian König765e7fb2016-09-15 15:06:50 +0200174 ret = amdgpu_bo_kmap(abo, NULL);
175 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176 if (ret) {
177 goto out_unref;
178 }
179
180 *gobj_p = gobj;
181 return 0;
182out_unref:
183 amdgpufb_destroy_pinned_object(gobj);
184 *gobj_p = NULL;
185 return ret;
186}
187
188static int amdgpufb_create(struct drm_fb_helper *helper,
189 struct drm_fb_helper_surface_size *sizes)
190{
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;
Christian König765e7fb2016-09-15 15:06:50 +0200197 struct amdgpu_bo *abo = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 int ret;
199 unsigned long tmp;
200
201 mode_cmd.width = sizes->surface_width;
202 mode_cmd.height = sizes->surface_height;
203
204 if (sizes->surface_bpp == 24)
205 sizes->surface_bpp = 32;
206
207 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
208 sizes->surface_depth);
209
210 ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
211 if (ret) {
212 DRM_ERROR("failed to create fbcon object %d\n", ret);
213 return ret;
214 }
215
Christian König765e7fb2016-09-15 15:06:50 +0200216 abo = gem_to_amdgpu_bo(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400217
218 /* okay we have an object now allocate the framebuffer */
Archit Taneja2dbaf3922015-07-31 16:22:00 +0530219 info = drm_fb_helper_alloc_fbi(helper);
220 if (IS_ERR(info)) {
221 ret = PTR_ERR(info);
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100222 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400223 }
224
225 info->par = rfbdev;
Alex Deucherdf7989f2015-11-02 10:52:32 -0500226 info->skip_vt_switch = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400227
228 ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
229 if (ret) {
230 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100231 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232 }
233
234 fb = &rfbdev->rfb.base;
235
236 /* setup helper */
237 rfbdev->helper.fb = fb;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400238
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400239 strcpy(info->fix.id, "amdgpudrmfb");
240
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200241 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400242
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400243 info->fbops = &amdgpufb_ops;
244
Christian König765e7fb2016-09-15 15:06:50 +0200245 tmp = amdgpu_bo_gpu_offset(abo) - adev->mc.vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400246 info->fix.smem_start = adev->mc.aper_base + tmp;
Christian König765e7fb2016-09-15 15:06:50 +0200247 info->fix.smem_len = amdgpu_bo_size(abo);
Christian Königf5e1c742017-07-20 23:45:18 +0200248 info->screen_base = amdgpu_bo_kptr(abo);
Christian König765e7fb2016-09-15 15:06:50 +0200249 info->screen_size = amdgpu_bo_size(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250
251 drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
252
253 /* setup aperture base/size for vesafb takeover */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254 info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
255 info->apertures->ranges[0].size = adev->mc.aper_size;
256
257 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
258
259 if (info->screen_base == NULL) {
260 ret = -ENOSPC;
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100261 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400262 }
263
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);
Christian König765e7fb2016-09-15 15:06:50 +0200266 DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(abo));
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200267 DRM_INFO("fb depth is %d\n", fb->format->depth);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400268 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
269
270 vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
271 return 0;
272
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100273out:
Christian König765e7fb2016-09-15 15:06:50 +0200274 if (abo) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400275
276 }
277 if (fb && ret) {
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300278 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400279 drm_framebuffer_unregister_private(fb);
280 drm_framebuffer_cleanup(fb);
281 kfree(fb);
282 }
283 return ret;
284}
285
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400286static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
287{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400288 struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
289
Archit Taneja2dbaf3922015-07-31 16:22:00 +0530290 drm_fb_helper_unregister_fbi(&rfbdev->helper);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400291
292 if (rfb->obj) {
293 amdgpufb_destroy_pinned_object(rfb->obj);
294 rfb->obj = NULL;
Michel Dänzera072c5f2017-09-11 17:04:41 +0900295 drm_framebuffer_unregister_private(&rfb->base);
296 drm_framebuffer_cleanup(&rfb->base);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400297 }
298 drm_fb_helper_fini(&rfbdev->helper);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400299
300 return 0;
301}
302
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400304 .fb_probe = amdgpufb_create,
305};
306
307int amdgpu_fbdev_init(struct amdgpu_device *adev)
308{
309 struct amdgpu_fbdev *rfbdev;
310 int bpp_sel = 32;
311 int ret;
312
313 /* don't init fbdev on hw without DCE */
314 if (!adev->mode_info.mode_config_initialized)
315 return 0;
316
Alex Deucherf49d45c2016-01-26 00:30:33 -0500317 /* don't init fbdev if there are no connectors */
318 if (list_empty(&adev->ddev->mode_config.connector_list))
319 return 0;
320
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400321 /* select 8 bpp console on low vram cards */
322 if (adev->mc.real_vram_size <= (32*1024*1024))
323 bpp_sel = 8;
324
325 rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
326 if (!rfbdev)
327 return -ENOMEM;
328
329 rfbdev->adev = adev;
330 adev->mode_info.rfbdev = rfbdev;
331
332 drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
333 &amdgpu_fb_helper_funcs);
334
335 ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400336 AMDGPUFB_CONN_LIMIT);
337 if (ret) {
338 kfree(rfbdev);
339 return ret;
340 }
341
342 drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
343
344 /* disable all the possible outputs/crtcs before entering KMS mode */
Andrey Grodzovsky93b8ca92017-05-30 16:49:59 -0400345 if (!amdgpu_device_has_dc_support(adev))
346 drm_helper_disable_unused_functions(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347
348 drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
349 return 0;
350}
351
352void amdgpu_fbdev_fini(struct amdgpu_device *adev)
353{
354 if (!adev->mode_info.rfbdev)
355 return;
356
357 amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
358 kfree(adev->mode_info.rfbdev);
359 adev->mode_info.rfbdev = NULL;
360}
361
362void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
363{
364 if (adev->mode_info.rfbdev)
Archit Taneja2dbaf3922015-07-31 16:22:00 +0530365 drm_fb_helper_set_suspend(&adev->mode_info.rfbdev->helper,
366 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400367}
368
369int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
370{
371 struct amdgpu_bo *robj;
372 int size = 0;
373
374 if (!adev->mode_info.rfbdev)
375 return 0;
376
377 robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj);
378 size += amdgpu_bo_size(robj);
379 return size;
380}
381
382bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
383{
384 if (!adev->mode_info.rfbdev)
385 return false;
386 if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj))
387 return true;
388 return false;
389}