blob: 47e0e2f6642d2520b7ca18fc25a4d3b731366a18 [file] [log] [blame]
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +02001/*
2 * drm kms/fb cma (contiguous memory allocator) helper functions
3 *
4 * Copyright (C) 2012 Analog Device Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
6 *
7 * Based on udl_fbdev.c
8 * Copyright (C) 2012 Red Hat
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <drm/drmP.h>
Noralf Trønnes894a6772018-07-03 18:03:50 +020021#include <drm/drm_client.h>
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020022#include <drm/drm_fb_helper.h>
Noralf Trønnes56286482017-08-13 15:31:45 +020023#include <drm/drm_framebuffer.h>
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020024#include <drm/drm_gem_cma_helper.h>
Noralf Trønnes56286482017-08-13 15:31:45 +020025#include <drm/drm_gem_framebuffer_helper.h>
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020026#include <drm/drm_fb_cma_helper.h>
Noralf Trønnes41b676e2017-11-15 15:19:41 +010027#include <drm/drm_print.h>
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020028#include <linux/module.h>
29
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020030struct drm_fbdev_cma {
31 struct drm_fb_helper fb_helper;
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020032};
33
Noralf Trønnes199c7712016-04-28 17:18:35 +020034/**
35 * DOC: framebuffer cma helper functions
36 *
37 * Provides helper functions for creating a cma (contiguous memory allocator)
38 * backed framebuffer.
39 *
Noralf Trønnesc0f095f2017-09-24 14:26:25 +020040 * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create
Noralf Trønnes02da16d2016-05-11 18:09:18 +020041 * callback function to create a cma backed framebuffer.
Noralf Trønnes199c7712016-04-28 17:18:35 +020042 *
43 * An fbdev framebuffer backed by cma is also available by calling
Noralf Trønnes41b676e2017-11-15 15:19:41 +010044 * drm_fb_cma_fbdev_init(). drm_fb_cma_fbdev_fini() tears it down.
Noralf Trønnes199c7712016-04-28 17:18:35 +020045 */
46
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020047static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
48{
49 return container_of(helper, struct drm_fbdev_cma, fb_helper);
50}
51
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020052/**
53 * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
54 * @fb: The framebuffer
55 * @plane: Which plane
56 *
57 * Return the CMA GEM object for given framebuffer.
58 *
59 * This function will usually be called from the CRTC callback functions.
60 */
61struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
Daniel Vetter890358a2016-05-31 23:11:12 +020062 unsigned int plane)
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020063{
Noralf Trønnes56286482017-08-13 15:31:45 +020064 struct drm_gem_object *gem;
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020065
Noralf Trønnes56286482017-08-13 15:31:45 +020066 gem = drm_gem_fb_get_obj(fb, plane);
67 if (!gem)
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020068 return NULL;
69
Noralf Trønnes56286482017-08-13 15:31:45 +020070 return to_drm_gem_cma_obj(gem);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +020071}
72EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
73
Marek Vasut14d7f962016-11-14 11:07:31 +010074/**
Yannick Fertre4636ce92017-04-14 12:13:32 +020075 * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer
76 * @fb: The framebuffer
77 * @state: Which state of drm plane
78 * @plane: Which plane
79 * Return the CMA GEM address for given framebuffer.
80 *
81 * This function will usually be called from the PLANE callback functions.
82 */
83dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
84 struct drm_plane_state *state,
85 unsigned int plane)
86{
Noralf Trønnes56286482017-08-13 15:31:45 +020087 struct drm_gem_cma_object *obj;
Yannick Fertre4636ce92017-04-14 12:13:32 +020088 dma_addr_t paddr;
Ayan Kumar Halderc76abab2018-08-17 17:54:00 +010089 u8 h_div = 1, v_div = 1;
Yannick Fertre4636ce92017-04-14 12:13:32 +020090
Noralf Trønnes56286482017-08-13 15:31:45 +020091 obj = drm_fb_cma_get_gem_obj(fb, plane);
92 if (!obj)
Yannick Fertre4636ce92017-04-14 12:13:32 +020093 return 0;
94
Noralf Trønnes56286482017-08-13 15:31:45 +020095 paddr = obj->paddr + fb->offsets[plane];
Ayan Kumar Halderc76abab2018-08-17 17:54:00 +010096
97 if (plane > 0) {
98 h_div = fb->format->hsub;
99 v_div = fb->format->vsub;
100 }
101
102 paddr += (fb->format->cpp[plane] * (state->src_x >> 16)) / h_div;
103 paddr += (fb->pitches[plane] * (state->src_y >> 16)) / v_div;
Yannick Fertre4636ce92017-04-14 12:13:32 +0200104
105 return paddr;
106}
107EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);
108
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200109/**
Noralf Trønnes41b676e2017-11-15 15:19:41 +0100110 * drm_fb_cma_fbdev_init() - Allocate and initialize fbdev emulation
111 * @dev: DRM device
112 * @preferred_bpp: Preferred bits per pixel for the device.
113 * @dev->mode_config.preferred_depth is used if this is zero.
114 * @max_conn_count: Maximum number of connectors.
115 * @dev->mode_config.num_connector is used if this is zero.
116 *
117 * Returns:
118 * Zero on success or negative error code on failure.
119 */
120int drm_fb_cma_fbdev_init(struct drm_device *dev, unsigned int preferred_bpp,
121 unsigned int max_conn_count)
122{
Noralf Trønnes894a6772018-07-03 18:03:50 +0200123 struct drm_fbdev_cma *fbdev_cma;
124
125 /* dev->fb_helper will indirectly point to fbdev_cma after this call */
126 fbdev_cma = drm_fbdev_cma_init(dev, preferred_bpp, max_conn_count);
127 if (IS_ERR(fbdev_cma))
128 return PTR_ERR(fbdev_cma);
129
130 return 0;
Noralf Trønnes41b676e2017-11-15 15:19:41 +0100131}
132EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_init);
133
134/**
135 * drm_fb_cma_fbdev_fini() - Teardown fbdev emulation
136 * @dev: DRM device
137 */
138void drm_fb_cma_fbdev_fini(struct drm_device *dev)
139{
Noralf Trønnes894a6772018-07-03 18:03:50 +0200140 if (dev->fb_helper)
141 drm_fbdev_cma_fini(to_fbdev_cma(dev->fb_helper));
Noralf Trønnes41b676e2017-11-15 15:19:41 +0100142}
143EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_fini);
144
Noralf Trønnes894a6772018-07-03 18:03:50 +0200145static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
146 .fb_probe = drm_fb_helper_generic_probe,
Noralf Trønnes56286482017-08-13 15:31:45 +0200147};
148
Noralf Trønnes199c7712016-04-28 17:18:35 +0200149/**
150 * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
151 * @dev: DRM device
152 * @preferred_bpp: Preferred bits per pixel for the device
Noralf Trønnes199c7712016-04-28 17:18:35 +0200153 * @max_conn_count: Maximum number of connectors
154 *
155 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
156 */
157struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200158 unsigned int preferred_bpp, unsigned int max_conn_count)
Noralf Trønnes199c7712016-04-28 17:18:35 +0200159{
Noralf Trønnes894a6772018-07-03 18:03:50 +0200160 struct drm_fbdev_cma *fbdev_cma;
161 struct drm_fb_helper *fb_helper;
162 int ret;
163
164 fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
165 if (!fbdev_cma)
166 return ERR_PTR(-ENOMEM);
167
168 fb_helper = &fbdev_cma->fb_helper;
169
170 ret = drm_client_new(dev, &fb_helper->client, "fbdev", NULL);
171 if (ret)
172 goto err_free;
173
174 ret = drm_fb_helper_fbdev_setup(dev, fb_helper, &drm_fb_cma_helper_funcs,
175 preferred_bpp, max_conn_count);
176 if (ret)
177 goto err_client_put;
178
179 return fbdev_cma;
180
181err_client_put:
182 drm_client_release(&fb_helper->client);
183err_free:
184 kfree(fbdev_cma);
185
186 return ERR_PTR(ret);
Noralf Trønnes199c7712016-04-28 17:18:35 +0200187}
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200188EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
189
190/**
191 * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
192 * @fbdev_cma: The drm_fbdev_cma struct
193 */
194void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
195{
Archit Taneja85f2edf2015-07-22 14:58:20 +0530196 drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
Noralf Trønnes894a6772018-07-03 18:03:50 +0200197 /* All resources have now been freed by drm_fbdev_fb_destroy() */
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200198}
199EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
200
201/**
202 * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
203 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
204 *
Daniel Vetter421242a2016-12-29 21:48:34 +0100205 * This function is usually called from the &drm_driver.lastclose callback.
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200206 */
207void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
208{
Rob Clark5ea1f752014-05-30 12:29:48 -0400209 if (fbdev_cma)
210 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200211}
212EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
213
214/**
215 * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
216 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
217 *
Daniel Vetter421242a2016-12-29 21:48:34 +0100218 * This function is usually called from the &drm_mode_config.output_poll_changed
Lars-Peter Clausen2e3b3c42012-07-02 16:37:47 +0200219 * callback.
220 */
221void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
222{
223 if (fbdev_cma)
224 drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
225}
226EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
Stefan Agner917f4252016-02-11 17:30:14 -0800227
228/**
Noralf Trønnesa4405b52017-01-22 19:11:09 +0100229 * drm_fbdev_cma_set_suspend_unlocked - wrapper around
230 * drm_fb_helper_set_suspend_unlocked
231 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
232 * @state: desired state, zero to resume, non-zero to suspend
233 *
234 * Calls drm_fb_helper_set_suspend, which is a wrapper around
235 * fb_set_suspend implemented by fbdev core.
236 */
237void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma,
Liviu Dudaud0a29872017-06-20 11:23:20 +0100238 bool state)
Noralf Trønnesa4405b52017-01-22 19:11:09 +0100239{
240 if (fbdev_cma)
241 drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper,
242 state);
243}
244EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked);