radeon_fb.c revision 1.11 1 /* $NetBSD: radeon_fb.c,v 1.11 2021/12/18 23:45:43 riastradh Exp $ */
2
3 /*
4 * Copyright 2007 David Airlie
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * David Airlie
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: radeon_fb.c,v 1.11 2021/12/18 23:45:43 riastradh Exp $");
31
32 #include <linux/module.h>
33 #include <linux/pci.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/slab.h>
36 #include <linux/vga_switcheroo.h>
37
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_crtc_helper.h>
40 #include <drm/drm_fb_helper.h>
41 #include <drm/drm_fourcc.h>
42 #include <drm/radeon_drm.h>
43
44 #include "radeon.h"
45
46 #ifdef __NetBSD__
47 #include "radeondrmkmsfb.h"
48
49 #include <linux/nbsd-namespace.h>
50 #endif
51
52 /* object hierarchy -
53 * this contains a helper + a radeon fb
54 * the helper contains a pointer to radeon framebuffer baseclass.
55 */
56 struct radeon_fbdev {
57 struct drm_fb_helper helper; /* must be first */
58 struct drm_framebuffer fb;
59 struct radeon_device *rdev;
60 };
61
62 static int
63 radeonfb_open(struct fb_info *info, int user)
64 {
65 struct radeon_fbdev *rfbdev = info->par;
66 struct radeon_device *rdev = rfbdev->rdev;
67 int ret = pm_runtime_get_sync(rdev->ddev->dev);
68 if (ret < 0 && ret != -EACCES) {
69 pm_runtime_mark_last_busy(rdev->ddev->dev);
70 pm_runtime_put_autosuspend(rdev->ddev->dev);
71 return ret;
72 }
73 return 0;
74 }
75
76 static int
77 radeonfb_release(struct fb_info *info, int user)
78 {
79 struct radeon_fbdev *rfbdev = info->par;
80 struct radeon_device *rdev = rfbdev->rdev;
81
82 pm_runtime_mark_last_busy(rdev->ddev->dev);
83 pm_runtime_put_autosuspend(rdev->ddev->dev);
84 return 0;
85 }
86
87 #ifndef __NetBSD__
88 static const struct fb_ops radeonfb_ops = {
89 .owner = THIS_MODULE,
90 DRM_FB_HELPER_DEFAULT_OPS,
91 .fb_open = radeonfb_open,
92 .fb_release = radeonfb_release,
93 .fb_fillrect = drm_fb_helper_cfb_fillrect,
94 .fb_copyarea = drm_fb_helper_cfb_copyarea,
95 .fb_imageblit = drm_fb_helper_cfb_imageblit,
96 };
97 #endif
98
99
100 int radeon_align_pitch(struct radeon_device *rdev, int width, int cpp, bool tiled)
101 {
102 int aligned = width;
103 int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
104 int pitch_mask = 0;
105
106 switch (cpp) {
107 case 1:
108 pitch_mask = align_large ? 255 : 127;
109 break;
110 case 2:
111 pitch_mask = align_large ? 127 : 31;
112 break;
113 case 3:
114 case 4:
115 pitch_mask = align_large ? 63 : 15;
116 break;
117 }
118
119 aligned += pitch_mask;
120 aligned &= ~pitch_mask;
121 return aligned * cpp;
122 }
123
124 static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
125 {
126 struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
127 int ret;
128
129 ret = radeon_bo_reserve(rbo, false);
130 if (likely(ret == 0)) {
131 radeon_bo_kunmap(rbo);
132 radeon_bo_unpin(rbo);
133 radeon_bo_unreserve(rbo);
134 }
135 drm_gem_object_put_unlocked(gobj);
136 }
137
138 static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
139 struct drm_mode_fb_cmd2 *mode_cmd,
140 struct drm_gem_object **gobj_p)
141 {
142 const struct drm_format_info *info;
143 struct radeon_device *rdev = rfbdev->rdev;
144 struct drm_gem_object *gobj = NULL;
145 struct radeon_bo *rbo = NULL;
146 bool fb_tiled = false; /* useful for testing */
147 u32 tiling_flags = 0;
148 int ret;
149 int aligned_size, size;
150 int height = mode_cmd->height;
151 u32 cpp;
152
153 info = drm_get_format_info(rdev->ddev, mode_cmd);
154 cpp = info->cpp[0];
155
156 /* need to align pitch with crtc limits */
157 mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, cpp,
158 fb_tiled);
159
160 if (rdev->family >= CHIP_R600)
161 height = ALIGN(mode_cmd->height, 8);
162 size = mode_cmd->pitches[0] * height;
163 aligned_size = ALIGN(size, PAGE_SIZE);
164 ret = radeon_gem_object_create(rdev, aligned_size, 0,
165 RADEON_GEM_DOMAIN_VRAM,
166 0, true, &gobj);
167 if (ret) {
168 pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
169 return -ENOMEM;
170 }
171 rbo = gem_to_radeon_bo(gobj);
172
173 if (fb_tiled)
174 tiling_flags = RADEON_TILING_MACRO;
175
176 #ifdef __BIG_ENDIAN
177 switch (cpp) {
178 case 4:
179 tiling_flags |= RADEON_TILING_SWAP_32BIT;
180 break;
181 case 2:
182 tiling_flags |= RADEON_TILING_SWAP_16BIT;
183 default:
184 break;
185 }
186 #endif
187
188 if (tiling_flags) {
189 ret = radeon_bo_set_tiling_flags(rbo,
190 tiling_flags | RADEON_TILING_SURFACE,
191 mode_cmd->pitches[0]);
192 if (ret)
193 dev_err(rdev->dev, "FB failed to set tiling flags\n");
194 }
195
196
197 ret = radeon_bo_reserve(rbo, false);
198 if (unlikely(ret != 0))
199 goto out_unref;
200 /* Only 27 bit offset for legacy CRTC */
201 ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
202 ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
203 NULL);
204 if (ret) {
205 radeon_bo_unreserve(rbo);
206 goto out_unref;
207 }
208 if (fb_tiled)
209 radeon_bo_check_tiling(rbo, 0, 0);
210 ret = radeon_bo_kmap(rbo, NULL);
211 radeon_bo_unreserve(rbo);
212 if (ret) {
213 goto out_unref;
214 }
215
216 *gobj_p = gobj;
217 return 0;
218 out_unref:
219 radeonfb_destroy_pinned_object(gobj);
220 *gobj_p = NULL;
221 return ret;
222 }
223
224 static int radeonfb_create(struct drm_fb_helper *helper,
225 struct drm_fb_helper_surface_size *sizes)
226 {
227 struct radeon_fbdev *rfbdev =
228 container_of(helper, struct radeon_fbdev, helper);
229 struct radeon_device *rdev = rfbdev->rdev;
230 #ifndef __NetBSD__
231 struct fb_info *info;
232 #endif
233 struct drm_framebuffer *fb = NULL;
234 struct drm_mode_fb_cmd2 mode_cmd;
235 struct drm_gem_object *gobj = NULL;
236 struct radeon_bo *rbo = NULL;
237 int ret;
238 #ifndef __NetBSD__
239 unsigned long tmp;
240 #endif
241
242 mode_cmd.width = sizes->surface_width;
243 mode_cmd.height = sizes->surface_height;
244
245 /* avivo can't scanout real 24bpp */
246 if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
247 sizes->surface_bpp = 32;
248
249 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
250 sizes->surface_depth);
251
252 ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
253 if (ret) {
254 DRM_ERROR("failed to create fbcon object %d\n", ret);
255 return ret;
256 }
257
258 rbo = gem_to_radeon_bo(gobj);
259
260 #ifdef __NetBSD__
261 ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
262 if (ret) {
263 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
264 goto out_unref;
265 }
266
267 (void)memset(rbo->kptr, 0, radeon_bo_size(rbo));
268
269 {
270 static const struct radeonfb_attach_args zero_rfa;
271 struct radeonfb_attach_args rfa = zero_rfa;
272
273 rfa.rfa_fb_helper = helper;
274 rfa.rfa_fb_sizes = *sizes;
275 rfa.rfa_fb_ptr = rbo->kptr;
276 rfa.rfa_fb_linebytes = mode_cmd.pitches[0];
277
278 helper->fbdev = config_found(rdev->ddev->dev, &rfa, NULL,
279 CFARGS(.iattr = "radeonfbbus"));
280 if (helper->fbdev == NULL) {
281 DRM_ERROR("failed to attach genfb\n");
282 goto out_unref;
283 }
284 }
285 fb = &rfbdev->rfb.base;
286 rfbdev->helper.fb = fb;
287 #else
288 /* okay we have an object now allocate the framebuffer */
289 info = drm_fb_helper_alloc_fbi(helper);
290 if (IS_ERR(info)) {
291 ret = PTR_ERR(info);
292 goto out;
293 }
294
295 /* radeon resume is fragile and needs a vt switch to help it along */
296 info->skip_vt_switch = false;
297
298 ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->fb, &mode_cmd, gobj);
299 if (ret) {
300 DRM_ERROR("failed to initialize framebuffer %d\n", ret);
301 goto out;
302 }
303
304 fb = &rfbdev->fb;
305
306 /* setup helper */
307 rfbdev->helper.fb = fb;
308
309 memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
310
311 info->fbops = &radeonfb_ops;
312
313 tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
314 info->fix.smem_start = rdev->mc.aper_base + tmp;
315 info->fix.smem_len = radeon_bo_size(rbo);
316 info->screen_base = rbo->kptr;
317 info->screen_size = radeon_bo_size(rbo);
318
319 drm_fb_helper_fill_info(info, &rfbdev->helper, sizes);
320
321 /* setup aperture base/size for vesafb takeover */
322 info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
323 info->apertures->ranges[0].size = rdev->mc.aper_size;
324
325 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
326
327 if (info->screen_base == NULL) {
328 ret = -ENOSPC;
329 goto out;
330 }
331
332 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
333 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
334 DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
335 DRM_INFO("fb depth is %d\n", fb->format->depth);
336 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
337
338 vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
339 #endif
340 return 0;
341
342 out:
343 if (rbo) {
344
345 }
346 if (fb && ret) {
347 drm_gem_object_put_unlocked(gobj);
348 drm_framebuffer_unregister_private(fb);
349 drm_framebuffer_cleanup(fb);
350 kfree(fb);
351 }
352 return ret;
353 }
354
355 static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
356 {
357 #ifdef __NetBSD__
358 int ret;
359 #endif
360 struct drm_framebuffer *fb = &rfbdev->fb;
361
362 #ifdef __NetBSD__
363 /* XXX errno NetBSD->Linux */
364 ret = -config_detach(rfbdev->helper.fbdev, DETACH_FORCE);
365 if (ret)
366 DRM_ERROR("failed to detach radeonfb: %d\n", ret);
367 rfbdev->helper.fbdev = NULL;
368 #else
369 drm_fb_helper_unregister_fbi(&rfbdev->helper);
370
371 if (fb->obj[0]) {
372 radeonfb_destroy_pinned_object(fb->obj[0]);
373 fb->obj[0] = NULL;
374 drm_framebuffer_unregister_private(fb);
375 drm_framebuffer_cleanup(fb);
376 }
377 drm_fb_helper_fini(&rfbdev->helper);
378 #endif
379
380 return 0;
381 }
382
383 static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
384 .fb_probe = radeonfb_create,
385 };
386
387 int radeon_fbdev_init(struct radeon_device *rdev)
388 {
389 struct radeon_fbdev *rfbdev;
390 int bpp_sel = 32;
391 int ret;
392
393 /* don't enable fbdev if no connectors */
394 if (list_empty(&rdev->ddev->mode_config.connector_list))
395 return 0;
396
397 /* select 8 bpp console on 8MB cards, or 16 bpp on RN50 or 32MB */
398 if (rdev->mc.real_vram_size <= (8*1024*1024))
399 bpp_sel = 8;
400 else if (ASIC_IS_RN50(rdev) ||
401 rdev->mc.real_vram_size <= (32*1024*1024))
402 bpp_sel = 16;
403
404 rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
405 if (!rfbdev)
406 return -ENOMEM;
407
408 rfbdev->rdev = rdev;
409 rdev->mode_info.rfbdev = rfbdev;
410
411 drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
412 &radeon_fb_helper_funcs);
413
414 ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
415 RADEONFB_CONN_LIMIT);
416 if (ret)
417 goto free;
418
419 ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
420 if (ret)
421 goto fini;
422
423 /* disable all the possible outputs/crtcs before entering KMS mode */
424 drm_helper_disable_unused_functions(rdev->ddev);
425
426 ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
427 if (ret)
428 goto fini;
429
430 return 0;
431
432 fini:
433 drm_fb_helper_fini(&rfbdev->helper);
434 free:
435 kfree(rfbdev);
436 return ret;
437 }
438
439 void radeon_fbdev_fini(struct radeon_device *rdev)
440 {
441 if (!rdev->mode_info.rfbdev)
442 return;
443
444 radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
445 kfree(rdev->mode_info.rfbdev);
446 rdev->mode_info.rfbdev = NULL;
447 }
448
449 void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
450 {
451 #ifndef __NetBSD__ /* XXX radeon fb suspend */
452 if (rdev->mode_info.rfbdev)
453 drm_fb_helper_set_suspend(&rdev->mode_info.rfbdev->helper, state);
454 #endif
455 }
456
457 bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
458 {
459 if (!rdev->mode_info.rfbdev)
460 return false;
461
462 if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->fb.obj[0]))
463 return true;
464 return false;
465 }
466
467 void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
468 {
469 if (rdev->mode_info.rfbdev)
470 drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
471 }
472
473 void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
474 {
475 if (rdev->mode_info.rfbdev)
476 drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
477 }
478