diff options
Diffstat (limited to 'drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c')
-rw-r--r-- | drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c | 421 |
1 files changed, 421 insertions, 0 deletions
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c new file mode 100644 index 000000000000..c1645d21f8e2 --- /dev/null +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fb.c | |||
@@ -0,0 +1,421 @@ | |||
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/slab.h> | ||
28 | #include <linux/fb.h> | ||
29 | |||
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" | ||
35 | #include "cikd.h" | ||
36 | |||
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 | */ | ||
45 | struct amdgpu_fbdev { | ||
46 | struct drm_fb_helper helper; | ||
47 | struct amdgpu_framebuffer rfb; | ||
48 | struct list_head fbdev_list; | ||
49 | struct amdgpu_device *adev; | ||
50 | }; | ||
51 | |||
52 | static struct fb_ops amdgpufb_ops = { | ||
53 | .owner = THIS_MODULE, | ||
54 | .fb_check_var = drm_fb_helper_check_var, | ||
55 | .fb_set_par = drm_fb_helper_set_par, | ||
56 | .fb_fillrect = cfb_fillrect, | ||
57 | .fb_copyarea = cfb_copyarea, | ||
58 | .fb_imageblit = cfb_imageblit, | ||
59 | .fb_pan_display = drm_fb_helper_pan_display, | ||
60 | .fb_blank = drm_fb_helper_blank, | ||
61 | .fb_setcmap = drm_fb_helper_setcmap, | ||
62 | .fb_debug_enter = drm_fb_helper_debug_enter, | ||
63 | .fb_debug_leave = drm_fb_helper_debug_leave, | ||
64 | }; | ||
65 | |||
66 | |||
67 | int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int bpp, bool tiled) | ||
68 | { | ||
69 | int aligned = width; | ||
70 | int pitch_mask = 0; | ||
71 | |||
72 | switch (bpp / 8) { | ||
73 | case 1: | ||
74 | pitch_mask = 255; | ||
75 | break; | ||
76 | case 2: | ||
77 | pitch_mask = 127; | ||
78 | break; | ||
79 | case 3: | ||
80 | case 4: | ||
81 | pitch_mask = 63; | ||
82 | break; | ||
83 | } | ||
84 | |||
85 | aligned += pitch_mask; | ||
86 | aligned &= ~pitch_mask; | ||
87 | return aligned; | ||
88 | } | ||
89 | |||
90 | static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj) | ||
91 | { | ||
92 | struct amdgpu_bo *rbo = gem_to_amdgpu_bo(gobj); | ||
93 | int ret; | ||
94 | |||
95 | ret = amdgpu_bo_reserve(rbo, false); | ||
96 | if (likely(ret == 0)) { | ||
97 | amdgpu_bo_kunmap(rbo); | ||
98 | amdgpu_bo_unpin(rbo); | ||
99 | amdgpu_bo_unreserve(rbo); | ||
100 | } | ||
101 | drm_gem_object_unreference_unlocked(gobj); | ||
102 | } | ||
103 | |||
104 | static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev, | ||
105 | struct drm_mode_fb_cmd2 *mode_cmd, | ||
106 | struct drm_gem_object **gobj_p) | ||
107 | { | ||
108 | struct amdgpu_device *adev = rfbdev->adev; | ||
109 | struct drm_gem_object *gobj = NULL; | ||
110 | struct amdgpu_bo *rbo = NULL; | ||
111 | bool fb_tiled = false; /* useful for testing */ | ||
112 | u32 tiling_flags = 0; | ||
113 | int ret; | ||
114 | int aligned_size, size; | ||
115 | int height = mode_cmd->height; | ||
116 | u32 bpp, depth; | ||
117 | |||
118 | drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp); | ||
119 | |||
120 | /* need to align pitch with crtc limits */ | ||
121 | mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, bpp, | ||
122 | fb_tiled) * ((bpp + 1) / 8); | ||
123 | |||
124 | height = ALIGN(mode_cmd->height, 8); | ||
125 | size = mode_cmd->pitches[0] * height; | ||
126 | aligned_size = ALIGN(size, PAGE_SIZE); | ||
127 | ret = amdgpu_gem_object_create(adev, aligned_size, 0, | ||
128 | AMDGPU_GEM_DOMAIN_VRAM, | ||
129 | 0, true, | ||
130 | &gobj); | ||
131 | if (ret) { | ||
132 | printk(KERN_ERR "failed to allocate framebuffer (%d)\n", | ||
133 | aligned_size); | ||
134 | return -ENOMEM; | ||
135 | } | ||
136 | rbo = gem_to_amdgpu_bo(gobj); | ||
137 | |||
138 | if (fb_tiled) | ||
139 | tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1); | ||
140 | |||
141 | ret = amdgpu_bo_reserve(rbo, false); | ||
142 | if (unlikely(ret != 0)) | ||
143 | goto out_unref; | ||
144 | |||
145 | if (tiling_flags) { | ||
146 | ret = amdgpu_bo_set_tiling_flags(rbo, | ||
147 | tiling_flags); | ||
148 | if (ret) | ||
149 | dev_err(adev->dev, "FB failed to set tiling flags\n"); | ||
150 | } | ||
151 | |||
152 | |||
153 | ret = amdgpu_bo_pin_restricted(rbo, AMDGPU_GEM_DOMAIN_VRAM, 0, 0, NULL); | ||
154 | if (ret) { | ||
155 | amdgpu_bo_unreserve(rbo); | ||
156 | goto out_unref; | ||
157 | } | ||
158 | ret = amdgpu_bo_kmap(rbo, NULL); | ||
159 | amdgpu_bo_unreserve(rbo); | ||
160 | if (ret) { | ||
161 | goto out_unref; | ||
162 | } | ||
163 | |||
164 | *gobj_p = gobj; | ||
165 | return 0; | ||
166 | out_unref: | ||
167 | amdgpufb_destroy_pinned_object(gobj); | ||
168 | *gobj_p = NULL; | ||
169 | return ret; | ||
170 | } | ||
171 | |||
172 | static int amdgpufb_create(struct drm_fb_helper *helper, | ||
173 | struct drm_fb_helper_surface_size *sizes) | ||
174 | { | ||
175 | struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper; | ||
176 | struct amdgpu_device *adev = rfbdev->adev; | ||
177 | struct fb_info *info; | ||
178 | struct drm_framebuffer *fb = NULL; | ||
179 | struct drm_mode_fb_cmd2 mode_cmd; | ||
180 | struct drm_gem_object *gobj = NULL; | ||
181 | struct amdgpu_bo *rbo = NULL; | ||
182 | struct device *device = &adev->pdev->dev; | ||
183 | int ret; | ||
184 | unsigned long tmp; | ||
185 | |||
186 | mode_cmd.width = sizes->surface_width; | ||
187 | mode_cmd.height = sizes->surface_height; | ||
188 | |||
189 | if (sizes->surface_bpp == 24) | ||
190 | sizes->surface_bpp = 32; | ||
191 | |||
192 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | ||
193 | sizes->surface_depth); | ||
194 | |||
195 | ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj); | ||
196 | if (ret) { | ||
197 | DRM_ERROR("failed to create fbcon object %d\n", ret); | ||
198 | return ret; | ||
199 | } | ||
200 | |||
201 | rbo = gem_to_amdgpu_bo(gobj); | ||
202 | |||
203 | /* okay we have an object now allocate the framebuffer */ | ||
204 | info = framebuffer_alloc(0, device); | ||
205 | if (info == NULL) { | ||
206 | ret = -ENOMEM; | ||
207 | goto out_unref; | ||
208 | } | ||
209 | |||
210 | info->par = rfbdev; | ||
211 | |||
212 | ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj); | ||
213 | if (ret) { | ||
214 | DRM_ERROR("failed to initialize framebuffer %d\n", ret); | ||
215 | goto out_unref; | ||
216 | } | ||
217 | |||
218 | fb = &rfbdev->rfb.base; | ||
219 | |||
220 | /* setup helper */ | ||
221 | rfbdev->helper.fb = fb; | ||
222 | rfbdev->helper.fbdev = info; | ||
223 | |||
224 | memset_io(rbo->kptr, 0x0, amdgpu_bo_size(rbo)); | ||
225 | |||
226 | strcpy(info->fix.id, "amdgpudrmfb"); | ||
227 | |||
228 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth); | ||
229 | |||
230 | info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT; | ||
231 | info->fbops = &amdgpufb_ops; | ||
232 | |||
233 | tmp = amdgpu_bo_gpu_offset(rbo) - adev->mc.vram_start; | ||
234 | info->fix.smem_start = adev->mc.aper_base + tmp; | ||
235 | info->fix.smem_len = amdgpu_bo_size(rbo); | ||
236 | info->screen_base = rbo->kptr; | ||
237 | info->screen_size = amdgpu_bo_size(rbo); | ||
238 | |||
239 | drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height); | ||
240 | |||
241 | /* setup aperture base/size for vesafb takeover */ | ||
242 | info->apertures = alloc_apertures(1); | ||
243 | if (!info->apertures) { | ||
244 | ret = -ENOMEM; | ||
245 | goto out_unref; | ||
246 | } | ||
247 | info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base; | ||
248 | info->apertures->ranges[0].size = adev->mc.aper_size; | ||
249 | |||
250 | /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ | ||
251 | |||
252 | if (info->screen_base == NULL) { | ||
253 | ret = -ENOSPC; | ||
254 | goto out_unref; | ||
255 | } | ||
256 | |||
257 | ret = fb_alloc_cmap(&info->cmap, 256, 0); | ||
258 | if (ret) { | ||
259 | ret = -ENOMEM; | ||
260 | goto out_unref; | ||
261 | } | ||
262 | |||
263 | DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start); | ||
264 | DRM_INFO("vram apper at 0x%lX\n", (unsigned long)adev->mc.aper_base); | ||
265 | DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(rbo)); | ||
266 | DRM_INFO("fb depth is %d\n", fb->depth); | ||
267 | DRM_INFO(" pitch is %d\n", fb->pitches[0]); | ||
268 | |||
269 | vga_switcheroo_client_fb_set(adev->ddev->pdev, info); | ||
270 | return 0; | ||
271 | |||
272 | out_unref: | ||
273 | if (rbo) { | ||
274 | |||
275 | } | ||
276 | if (fb && ret) { | ||
277 | drm_gem_object_unreference(gobj); | ||
278 | drm_framebuffer_unregister_private(fb); | ||
279 | drm_framebuffer_cleanup(fb); | ||
280 | kfree(fb); | ||
281 | } | ||
282 | return ret; | ||
283 | } | ||
284 | |||
285 | void amdgpu_fb_output_poll_changed(struct amdgpu_device *adev) | ||
286 | { | ||
287 | if (adev->mode_info.rfbdev) | ||
288 | drm_fb_helper_hotplug_event(&adev->mode_info.rfbdev->helper); | ||
289 | } | ||
290 | |||
291 | static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev) | ||
292 | { | ||
293 | struct fb_info *info; | ||
294 | struct amdgpu_framebuffer *rfb = &rfbdev->rfb; | ||
295 | |||
296 | if (rfbdev->helper.fbdev) { | ||
297 | info = rfbdev->helper.fbdev; | ||
298 | |||
299 | unregister_framebuffer(info); | ||
300 | if (info->cmap.len) | ||
301 | fb_dealloc_cmap(&info->cmap); | ||
302 | framebuffer_release(info); | ||
303 | } | ||
304 | |||
305 | if (rfb->obj) { | ||
306 | amdgpufb_destroy_pinned_object(rfb->obj); | ||
307 | rfb->obj = NULL; | ||
308 | } | ||
309 | drm_fb_helper_fini(&rfbdev->helper); | ||
310 | drm_framebuffer_unregister_private(&rfb->base); | ||
311 | drm_framebuffer_cleanup(&rfb->base); | ||
312 | |||
313 | return 0; | ||
314 | } | ||
315 | |||
316 | /** Sets the color ramps on behalf of fbcon */ | ||
317 | static void amdgpu_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green, | ||
318 | u16 blue, int regno) | ||
319 | { | ||
320 | struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); | ||
321 | |||
322 | amdgpu_crtc->lut_r[regno] = red >> 6; | ||
323 | amdgpu_crtc->lut_g[regno] = green >> 6; | ||
324 | amdgpu_crtc->lut_b[regno] = blue >> 6; | ||
325 | } | ||
326 | |||
327 | /** Gets the color ramps on behalf of fbcon */ | ||
328 | static void amdgpu_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green, | ||
329 | u16 *blue, int regno) | ||
330 | { | ||
331 | struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); | ||
332 | |||
333 | *red = amdgpu_crtc->lut_r[regno] << 6; | ||
334 | *green = amdgpu_crtc->lut_g[regno] << 6; | ||
335 | *blue = amdgpu_crtc->lut_b[regno] << 6; | ||
336 | } | ||
337 | |||
338 | static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = { | ||
339 | .gamma_set = amdgpu_crtc_fb_gamma_set, | ||
340 | .gamma_get = amdgpu_crtc_fb_gamma_get, | ||
341 | .fb_probe = amdgpufb_create, | ||
342 | }; | ||
343 | |||
344 | int amdgpu_fbdev_init(struct amdgpu_device *adev) | ||
345 | { | ||
346 | struct amdgpu_fbdev *rfbdev; | ||
347 | int bpp_sel = 32; | ||
348 | int ret; | ||
349 | |||
350 | /* don't init fbdev on hw without DCE */ | ||
351 | if (!adev->mode_info.mode_config_initialized) | ||
352 | return 0; | ||
353 | |||
354 | /* select 8 bpp console on low vram cards */ | ||
355 | if (adev->mc.real_vram_size <= (32*1024*1024)) | ||
356 | bpp_sel = 8; | ||
357 | |||
358 | rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL); | ||
359 | if (!rfbdev) | ||
360 | return -ENOMEM; | ||
361 | |||
362 | rfbdev->adev = adev; | ||
363 | adev->mode_info.rfbdev = rfbdev; | ||
364 | |||
365 | drm_fb_helper_prepare(adev->ddev, &rfbdev->helper, | ||
366 | &amdgpu_fb_helper_funcs); | ||
367 | |||
368 | ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper, | ||
369 | adev->mode_info.num_crtc, | ||
370 | AMDGPUFB_CONN_LIMIT); | ||
371 | if (ret) { | ||
372 | kfree(rfbdev); | ||
373 | return ret; | ||
374 | } | ||
375 | |||
376 | drm_fb_helper_single_add_all_connectors(&rfbdev->helper); | ||
377 | |||
378 | /* disable all the possible outputs/crtcs before entering KMS mode */ | ||
379 | drm_helper_disable_unused_functions(adev->ddev); | ||
380 | |||
381 | drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel); | ||
382 | return 0; | ||
383 | } | ||
384 | |||
385 | void amdgpu_fbdev_fini(struct amdgpu_device *adev) | ||
386 | { | ||
387 | if (!adev->mode_info.rfbdev) | ||
388 | return; | ||
389 | |||
390 | amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev); | ||
391 | kfree(adev->mode_info.rfbdev); | ||
392 | adev->mode_info.rfbdev = NULL; | ||
393 | } | ||
394 | |||
395 | void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state) | ||
396 | { | ||
397 | if (adev->mode_info.rfbdev) | ||
398 | fb_set_suspend(adev->mode_info.rfbdev->helper.fbdev, state); | ||
399 | } | ||
400 | |||
401 | int amdgpu_fbdev_total_size(struct amdgpu_device *adev) | ||
402 | { | ||
403 | struct amdgpu_bo *robj; | ||
404 | int size = 0; | ||
405 | |||
406 | if (!adev->mode_info.rfbdev) | ||
407 | return 0; | ||
408 | |||
409 | robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj); | ||
410 | size += amdgpu_bo_size(robj); | ||
411 | return size; | ||
412 | } | ||
413 | |||
414 | bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj) | ||
415 | { | ||
416 | if (!adev->mode_info.rfbdev) | ||
417 | return false; | ||
418 | if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj)) | ||
419 | return true; | ||
420 | return false; | ||
421 | } | ||