Emmanuel Vadot | df0e7f7 | 2020-02-15 19:09:10 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 or MIT |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2018 Noralf Trønnes |
| 4 | */ |
| 5 | |
Thomas Zimmermann | a745fb1 | 2020-11-03 10:30:12 +0100 | [diff] [blame] | 6 | #include <linux/dma-buf-map.h> |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 7 | #include <linux/list.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/mutex.h> |
| 10 | #include <linux/seq_file.h> |
| 11 | #include <linux/slab.h> |
| 12 | |
| 13 | #include <drm/drm_client.h> |
| 14 | #include <drm/drm_debugfs.h> |
| 15 | #include <drm/drm_device.h> |
| 16 | #include <drm/drm_drv.h> |
| 17 | #include <drm/drm_file.h> |
| 18 | #include <drm/drm_fourcc.h> |
Sam Ravnborg | 0500c04 | 2019-05-26 19:35:35 +0200 | [diff] [blame] | 19 | #include <drm/drm_framebuffer.h> |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 20 | #include <drm/drm_gem.h> |
| 21 | #include <drm/drm_mode.h> |
| 22 | #include <drm/drm_print.h> |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 23 | |
| 24 | #include "drm_crtc_internal.h" |
| 25 | #include "drm_internal.h" |
| 26 | |
| 27 | /** |
| 28 | * DOC: overview |
| 29 | * |
| 30 | * This library provides support for clients running in the kernel like fbdev and bootsplash. |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 31 | * |
| 32 | * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported. |
| 33 | */ |
| 34 | |
| 35 | static int drm_client_open(struct drm_client_dev *client) |
| 36 | { |
| 37 | struct drm_device *dev = client->dev; |
| 38 | struct drm_file *file; |
| 39 | |
| 40 | file = drm_file_alloc(dev->primary); |
| 41 | if (IS_ERR(file)) |
| 42 | return PTR_ERR(file); |
| 43 | |
| 44 | mutex_lock(&dev->filelist_mutex); |
| 45 | list_add(&file->lhead, &dev->filelist_internal); |
| 46 | mutex_unlock(&dev->filelist_mutex); |
| 47 | |
| 48 | client->file = file; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static void drm_client_close(struct drm_client_dev *client) |
| 54 | { |
| 55 | struct drm_device *dev = client->dev; |
| 56 | |
| 57 | mutex_lock(&dev->filelist_mutex); |
| 58 | list_del(&client->file->lhead); |
| 59 | mutex_unlock(&dev->filelist_mutex); |
| 60 | |
| 61 | drm_file_free(client->file); |
| 62 | } |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 63 | |
| 64 | /** |
Noralf Trønnes | 4d4c2d8 | 2018-10-01 21:45:36 +0200 | [diff] [blame] | 65 | * drm_client_init - Initialise a DRM client |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 66 | * @dev: DRM device |
| 67 | * @client: DRM client |
| 68 | * @name: Client name |
| 69 | * @funcs: DRM client functions (optional) |
| 70 | * |
Noralf Trønnes | e33898a | 2019-04-03 14:56:58 +0200 | [diff] [blame] | 71 | * This initialises the client and opens a &drm_file. |
| 72 | * Use drm_client_register() to complete the process. |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 73 | * The caller needs to hold a reference on @dev before calling this function. |
| 74 | * The client is freed when the &drm_device is unregistered. See drm_client_release(). |
| 75 | * |
| 76 | * Returns: |
| 77 | * Zero on success or negative error code on failure. |
| 78 | */ |
Noralf Trønnes | 4d4c2d8 | 2018-10-01 21:45:36 +0200 | [diff] [blame] | 79 | int drm_client_init(struct drm_device *dev, struct drm_client_dev *client, |
| 80 | const char *name, const struct drm_client_funcs *funcs) |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 81 | { |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 82 | int ret; |
| 83 | |
Noralf Trønnes | b39b539 | 2018-11-10 15:56:45 +0100 | [diff] [blame] | 84 | if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create) |
Chris Wilson | 69fdf42 | 2018-09-13 20:20:50 +0100 | [diff] [blame] | 85 | return -EOPNOTSUPP; |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 86 | |
| 87 | if (funcs && !try_module_get(funcs->owner)) |
| 88 | return -ENODEV; |
| 89 | |
| 90 | client->dev = dev; |
| 91 | client->name = name; |
| 92 | client->funcs = funcs; |
| 93 | |
Noralf Trønnes | d81294a | 2019-05-31 16:01:11 +0200 | [diff] [blame] | 94 | ret = drm_client_modeset_create(client); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 95 | if (ret) |
| 96 | goto err_put_module; |
| 97 | |
Noralf Trønnes | d81294a | 2019-05-31 16:01:11 +0200 | [diff] [blame] | 98 | ret = drm_client_open(client); |
| 99 | if (ret) |
| 100 | goto err_free; |
| 101 | |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 102 | drm_dev_get(dev); |
| 103 | |
| 104 | return 0; |
| 105 | |
Noralf Trønnes | d81294a | 2019-05-31 16:01:11 +0200 | [diff] [blame] | 106 | err_free: |
| 107 | drm_client_modeset_free(client); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 108 | err_put_module: |
| 109 | if (funcs) |
| 110 | module_put(funcs->owner); |
| 111 | |
| 112 | return ret; |
| 113 | } |
Noralf Trønnes | 4d4c2d8 | 2018-10-01 21:45:36 +0200 | [diff] [blame] | 114 | EXPORT_SYMBOL(drm_client_init); |
| 115 | |
| 116 | /** |
Noralf Trønnes | e33898a | 2019-04-03 14:56:58 +0200 | [diff] [blame] | 117 | * drm_client_register - Register client |
Noralf Trønnes | 4d4c2d8 | 2018-10-01 21:45:36 +0200 | [diff] [blame] | 118 | * @client: DRM client |
| 119 | * |
| 120 | * Add the client to the &drm_device client list to activate its callbacks. |
| 121 | * @client must be initialized by a call to drm_client_init(). After |
Noralf Trønnes | e33898a | 2019-04-03 14:56:58 +0200 | [diff] [blame] | 122 | * drm_client_register() it is no longer permissible to call drm_client_release() |
Noralf Trønnes | 4d4c2d8 | 2018-10-01 21:45:36 +0200 | [diff] [blame] | 123 | * directly (outside the unregister callback), instead cleanup will happen |
| 124 | * automatically on driver unload. |
| 125 | */ |
Noralf Trønnes | e33898a | 2019-04-03 14:56:58 +0200 | [diff] [blame] | 126 | void drm_client_register(struct drm_client_dev *client) |
Noralf Trønnes | 4d4c2d8 | 2018-10-01 21:45:36 +0200 | [diff] [blame] | 127 | { |
| 128 | struct drm_device *dev = client->dev; |
| 129 | |
| 130 | mutex_lock(&dev->clientlist_mutex); |
| 131 | list_add(&client->list, &dev->clientlist); |
| 132 | mutex_unlock(&dev->clientlist_mutex); |
| 133 | } |
Noralf Trønnes | e33898a | 2019-04-03 14:56:58 +0200 | [diff] [blame] | 134 | EXPORT_SYMBOL(drm_client_register); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * drm_client_release - Release DRM client resources |
| 138 | * @client: DRM client |
| 139 | * |
Noralf Trønnes | 4d4c2d8 | 2018-10-01 21:45:36 +0200 | [diff] [blame] | 140 | * Releases resources by closing the &drm_file that was opened by drm_client_init(). |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 141 | * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set. |
| 142 | * |
| 143 | * This function should only be called from the unregister callback. An exception |
| 144 | * is fbdev which cannot free the buffer if userspace has open file descriptors. |
| 145 | * |
| 146 | * Note: |
| 147 | * Clients cannot initiate a release by themselves. This is done to keep the code simple. |
| 148 | * The driver has to be unloaded before the client can be unloaded. |
| 149 | */ |
| 150 | void drm_client_release(struct drm_client_dev *client) |
| 151 | { |
| 152 | struct drm_device *dev = client->dev; |
| 153 | |
Jani Nikula | 41cb660 | 2019-12-10 14:30:44 +0200 | [diff] [blame] | 154 | drm_dbg_kms(dev, "%s\n", client->name); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 155 | |
Noralf Trønnes | d81294a | 2019-05-31 16:01:11 +0200 | [diff] [blame] | 156 | drm_client_modeset_free(client); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 157 | drm_client_close(client); |
| 158 | drm_dev_put(dev); |
| 159 | if (client->funcs) |
| 160 | module_put(client->funcs->owner); |
| 161 | } |
| 162 | EXPORT_SYMBOL(drm_client_release); |
| 163 | |
| 164 | void drm_client_dev_unregister(struct drm_device *dev) |
| 165 | { |
| 166 | struct drm_client_dev *client, *tmp; |
| 167 | |
| 168 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 169 | return; |
| 170 | |
| 171 | mutex_lock(&dev->clientlist_mutex); |
| 172 | list_for_each_entry_safe(client, tmp, &dev->clientlist, list) { |
| 173 | list_del(&client->list); |
| 174 | if (client->funcs && client->funcs->unregister) { |
| 175 | client->funcs->unregister(client); |
| 176 | } else { |
| 177 | drm_client_release(client); |
| 178 | kfree(client); |
| 179 | } |
| 180 | } |
| 181 | mutex_unlock(&dev->clientlist_mutex); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * drm_client_dev_hotplug - Send hotplug event to clients |
| 186 | * @dev: DRM device |
| 187 | * |
| 188 | * This function calls the &drm_client_funcs.hotplug callback on the attached clients. |
| 189 | * |
| 190 | * drm_kms_helper_hotplug_event() calls this function, so drivers that use it |
| 191 | * don't need to call this function themselves. |
| 192 | */ |
| 193 | void drm_client_dev_hotplug(struct drm_device *dev) |
| 194 | { |
| 195 | struct drm_client_dev *client; |
| 196 | int ret; |
| 197 | |
| 198 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 199 | return; |
| 200 | |
| 201 | mutex_lock(&dev->clientlist_mutex); |
| 202 | list_for_each_entry(client, &dev->clientlist, list) { |
| 203 | if (!client->funcs || !client->funcs->hotplug) |
| 204 | continue; |
| 205 | |
| 206 | ret = client->funcs->hotplug(client); |
Jani Nikula | 41cb660 | 2019-12-10 14:30:44 +0200 | [diff] [blame] | 207 | drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 208 | } |
| 209 | mutex_unlock(&dev->clientlist_mutex); |
| 210 | } |
| 211 | EXPORT_SYMBOL(drm_client_dev_hotplug); |
| 212 | |
| 213 | void drm_client_dev_restore(struct drm_device *dev) |
| 214 | { |
| 215 | struct drm_client_dev *client; |
| 216 | int ret; |
| 217 | |
| 218 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 219 | return; |
| 220 | |
| 221 | mutex_lock(&dev->clientlist_mutex); |
| 222 | list_for_each_entry(client, &dev->clientlist, list) { |
| 223 | if (!client->funcs || !client->funcs->restore) |
| 224 | continue; |
| 225 | |
| 226 | ret = client->funcs->restore(client); |
Jani Nikula | 41cb660 | 2019-12-10 14:30:44 +0200 | [diff] [blame] | 227 | drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 228 | if (!ret) /* The first one to return zero gets the privilege to restore */ |
| 229 | break; |
| 230 | } |
| 231 | mutex_unlock(&dev->clientlist_mutex); |
| 232 | } |
| 233 | |
| 234 | static void drm_client_buffer_delete(struct drm_client_buffer *buffer) |
| 235 | { |
| 236 | struct drm_device *dev = buffer->client->dev; |
| 237 | |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 238 | drm_gem_vunmap(buffer->gem, &buffer->map); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 239 | |
| 240 | if (buffer->gem) |
Emil Velikov | be6ee10 | 2020-05-15 10:50:53 +0100 | [diff] [blame] | 241 | drm_gem_object_put(buffer->gem); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 242 | |
Noralf Trønnes | cf19fa2 | 2018-07-12 17:04:14 +0200 | [diff] [blame] | 243 | if (buffer->handle) |
| 244 | drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file); |
| 245 | |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 246 | kfree(buffer); |
| 247 | } |
| 248 | |
| 249 | static struct drm_client_buffer * |
| 250 | drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format) |
| 251 | { |
Maxime Ripard | 24c478e | 2019-05-16 12:31:49 +0200 | [diff] [blame] | 252 | const struct drm_format_info *info = drm_format_info(format); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 253 | struct drm_mode_create_dumb dumb_args = { }; |
| 254 | struct drm_device *dev = client->dev; |
| 255 | struct drm_client_buffer *buffer; |
| 256 | struct drm_gem_object *obj; |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 257 | int ret; |
| 258 | |
| 259 | buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); |
| 260 | if (!buffer) |
| 261 | return ERR_PTR(-ENOMEM); |
| 262 | |
| 263 | buffer->client = client; |
| 264 | |
| 265 | dumb_args.width = width; |
| 266 | dumb_args.height = height; |
Maxime Ripard | b0f986b | 2019-05-16 12:31:52 +0200 | [diff] [blame] | 267 | dumb_args.bpp = info->cpp[0] * 8; |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 268 | ret = drm_mode_create_dumb(dev, &dumb_args, client->file); |
| 269 | if (ret) |
Noralf Trønnes | cf19fa2 | 2018-07-12 17:04:14 +0200 | [diff] [blame] | 270 | goto err_delete; |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 271 | |
| 272 | buffer->handle = dumb_args.handle; |
| 273 | buffer->pitch = dumb_args.pitch; |
| 274 | |
| 275 | obj = drm_gem_object_lookup(client->file, dumb_args.handle); |
| 276 | if (!obj) { |
| 277 | ret = -ENOENT; |
| 278 | goto err_delete; |
| 279 | } |
| 280 | |
| 281 | buffer->gem = obj; |
| 282 | |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 283 | return buffer; |
| 284 | |
| 285 | err_delete: |
| 286 | drm_client_buffer_delete(buffer); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 287 | |
| 288 | return ERR_PTR(ret); |
| 289 | } |
| 290 | |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 291 | /** |
| 292 | * drm_client_buffer_vmap - Map DRM client buffer into address space |
| 293 | * @buffer: DRM client buffer |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 294 | * @map_copy: Returns the mapped memory's address |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 295 | * |
| 296 | * This function maps a client buffer into kernel address space. If the |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 297 | * buffer is already mapped, it returns the existing mapping's address. |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 298 | * |
| 299 | * Client buffer mappings are not ref'counted. Each call to |
| 300 | * drm_client_buffer_vmap() should be followed by a call to |
| 301 | * drm_client_buffer_vunmap(); or the client buffer should be mapped |
Thomas Zimmermann | cf1ca9a | 2019-07-03 09:58:24 +0200 | [diff] [blame] | 302 | * throughout its lifetime. |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 303 | * |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 304 | * The returned address is a copy of the internal value. In contrast to |
| 305 | * other vmap interfaces, you don't need it for the client's vunmap |
| 306 | * function. So you can modify it at will during blit and draw operations. |
| 307 | * |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 308 | * Returns: |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 309 | * 0 on success, or a negative errno code otherwise. |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 310 | */ |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 311 | int |
| 312 | drm_client_buffer_vmap(struct drm_client_buffer *buffer, struct dma_buf_map *map_copy) |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 313 | { |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 314 | struct dma_buf_map *map = &buffer->map; |
Thomas Zimmermann | a745fb1 | 2020-11-03 10:30:12 +0100 | [diff] [blame] | 315 | int ret; |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 316 | |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 317 | /* |
| 318 | * FIXME: The dependency on GEM here isn't required, we could |
| 319 | * convert the driver handle to a dma-buf instead and use the |
| 320 | * backend-agnostic dma-buf vmap support instead. This would |
| 321 | * require that the handle2fd prime ioctl is reworked to pull the |
| 322 | * fd_install step out of the driver backend hooks, to make that |
| 323 | * final step optional for internal users. |
| 324 | */ |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 325 | ret = drm_gem_vmap(buffer->gem, map); |
Thomas Zimmermann | a745fb1 | 2020-11-03 10:30:12 +0100 | [diff] [blame] | 326 | if (ret) |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 327 | return ret; |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 328 | |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 329 | *map_copy = *map; |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 330 | |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 331 | return 0; |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 332 | } |
| 333 | EXPORT_SYMBOL(drm_client_buffer_vmap); |
| 334 | |
| 335 | /** |
| 336 | * drm_client_buffer_vunmap - Unmap DRM client buffer |
| 337 | * @buffer: DRM client buffer |
| 338 | * |
Thomas Zimmermann | cf1ca9a | 2019-07-03 09:58:24 +0200 | [diff] [blame] | 339 | * This function removes a client buffer's memory mapping. Calling this |
| 340 | * function is only required by clients that manage their buffer mappings |
| 341 | * by themselves. |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 342 | */ |
| 343 | void drm_client_buffer_vunmap(struct drm_client_buffer *buffer) |
| 344 | { |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 345 | struct dma_buf_map *map = &buffer->map; |
Thomas Zimmermann | a745fb1 | 2020-11-03 10:30:12 +0100 | [diff] [blame] | 346 | |
Thomas Zimmermann | a859555 | 2020-11-03 10:30:13 +0100 | [diff] [blame] | 347 | drm_gem_vunmap(buffer->gem, map); |
Thomas Zimmermann | 15dd0fc | 2019-07-03 09:58:18 +0200 | [diff] [blame] | 348 | } |
| 349 | EXPORT_SYMBOL(drm_client_buffer_vunmap); |
| 350 | |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 351 | static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer) |
| 352 | { |
| 353 | int ret; |
| 354 | |
| 355 | if (!buffer->fb) |
| 356 | return; |
| 357 | |
| 358 | ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file); |
| 359 | if (ret) |
Jani Nikula | 41cb660 | 2019-12-10 14:30:44 +0200 | [diff] [blame] | 360 | drm_err(buffer->client->dev, |
| 361 | "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret); |
Noralf Trønnes | c76f0f7 | 2018-07-03 18:03:47 +0200 | [diff] [blame] | 362 | |
| 363 | buffer->fb = NULL; |
| 364 | } |
| 365 | |
| 366 | static int drm_client_buffer_addfb(struct drm_client_buffer *buffer, |
| 367 | u32 width, u32 height, u32 format) |
| 368 | { |
| 369 | struct drm_client_dev *client = buffer->client; |
| 370 | struct drm_mode_fb_cmd fb_req = { }; |
| 371 | const struct drm_format_info *info; |
| 372 | int ret; |
| 373 | |
| 374 | info = drm_format_info(format); |
| 375 | fb_req.bpp = info->cpp[0] * 8; |
| 376 | fb_req.depth = info->depth; |
| 377 | fb_req.width = width; |
| 378 | fb_req.height = height; |
| 379 | fb_req.handle = buffer->handle; |
| 380 | fb_req.pitch = buffer->pitch; |
| 381 | |
| 382 | ret = drm_mode_addfb(client->dev, &fb_req, client->file); |
| 383 | if (ret) |
| 384 | return ret; |
| 385 | |
| 386 | buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id); |
| 387 | if (WARN_ON(!buffer->fb)) |
| 388 | return -ENOENT; |
| 389 | |
| 390 | /* drop the reference we picked up in framebuffer lookup */ |
| 391 | drm_framebuffer_put(buffer->fb); |
| 392 | |
| 393 | strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN); |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * drm_client_framebuffer_create - Create a client framebuffer |
| 400 | * @client: DRM client |
| 401 | * @width: Framebuffer width |
| 402 | * @height: Framebuffer height |
| 403 | * @format: Buffer format |
| 404 | * |
| 405 | * This function creates a &drm_client_buffer which consists of a |
| 406 | * &drm_framebuffer backed by a dumb buffer. |
| 407 | * Call drm_client_framebuffer_delete() to free the buffer. |
| 408 | * |
| 409 | * Returns: |
| 410 | * Pointer to a client buffer or an error pointer on failure. |
| 411 | */ |
| 412 | struct drm_client_buffer * |
| 413 | drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format) |
| 414 | { |
| 415 | struct drm_client_buffer *buffer; |
| 416 | int ret; |
| 417 | |
| 418 | buffer = drm_client_buffer_create(client, width, height, format); |
| 419 | if (IS_ERR(buffer)) |
| 420 | return buffer; |
| 421 | |
| 422 | ret = drm_client_buffer_addfb(buffer, width, height, format); |
| 423 | if (ret) { |
| 424 | drm_client_buffer_delete(buffer); |
| 425 | return ERR_PTR(ret); |
| 426 | } |
| 427 | |
| 428 | return buffer; |
| 429 | } |
| 430 | EXPORT_SYMBOL(drm_client_framebuffer_create); |
| 431 | |
| 432 | /** |
| 433 | * drm_client_framebuffer_delete - Delete a client framebuffer |
| 434 | * @buffer: DRM client buffer (can be NULL) |
| 435 | */ |
| 436 | void drm_client_framebuffer_delete(struct drm_client_buffer *buffer) |
| 437 | { |
| 438 | if (!buffer) |
| 439 | return; |
| 440 | |
| 441 | drm_client_buffer_rmfb(buffer); |
| 442 | drm_client_buffer_delete(buffer); |
| 443 | } |
| 444 | EXPORT_SYMBOL(drm_client_framebuffer_delete); |
Noralf Trønnes | e896c132 | 2018-07-03 18:03:51 +0200 | [diff] [blame] | 445 | |
Noralf Trønnes | c9c03e3 | 2020-05-09 16:16:12 +0200 | [diff] [blame] | 446 | /** |
| 447 | * drm_client_framebuffer_flush - Manually flush client framebuffer |
| 448 | * @buffer: DRM client buffer (can be NULL) |
| 449 | * @rect: Damage rectangle (if NULL flushes all) |
| 450 | * |
| 451 | * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes |
| 452 | * for drivers that need it. |
| 453 | * |
| 454 | * Returns: |
| 455 | * Zero on success or negative error code on failure. |
| 456 | */ |
| 457 | int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect) |
| 458 | { |
| 459 | if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty) |
| 460 | return 0; |
| 461 | |
| 462 | if (rect) { |
| 463 | struct drm_clip_rect clip = { |
| 464 | .x1 = rect->x1, |
| 465 | .y1 = rect->y1, |
| 466 | .x2 = rect->x2, |
| 467 | .y2 = rect->y2, |
| 468 | }; |
| 469 | |
| 470 | return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file, |
| 471 | 0, 0, &clip, 1); |
| 472 | } |
| 473 | |
| 474 | return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file, |
| 475 | 0, 0, NULL, 0); |
| 476 | } |
| 477 | EXPORT_SYMBOL(drm_client_framebuffer_flush); |
| 478 | |
Noralf Trønnes | e896c132 | 2018-07-03 18:03:51 +0200 | [diff] [blame] | 479 | #ifdef CONFIG_DEBUG_FS |
| 480 | static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data) |
| 481 | { |
| 482 | struct drm_info_node *node = m->private; |
| 483 | struct drm_device *dev = node->minor->dev; |
| 484 | struct drm_printer p = drm_seq_file_printer(m); |
| 485 | struct drm_client_dev *client; |
| 486 | |
| 487 | mutex_lock(&dev->clientlist_mutex); |
| 488 | list_for_each_entry(client, &dev->clientlist, list) |
| 489 | drm_printf(&p, "%s\n", client->name); |
| 490 | mutex_unlock(&dev->clientlist_mutex); |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static const struct drm_info_list drm_client_debugfs_list[] = { |
| 496 | { "internal_clients", drm_client_debugfs_internal_clients, 0 }, |
| 497 | }; |
| 498 | |
Wambui Karuga | 7ce84471 | 2020-03-10 16:31:21 +0300 | [diff] [blame] | 499 | void drm_client_debugfs_init(struct drm_minor *minor) |
Noralf Trønnes | e896c132 | 2018-07-03 18:03:51 +0200 | [diff] [blame] | 500 | { |
Wambui Karuga | e196e14 | 2020-03-10 16:31:19 +0300 | [diff] [blame] | 501 | drm_debugfs_create_files(drm_client_debugfs_list, |
| 502 | ARRAY_SIZE(drm_client_debugfs_list), |
| 503 | minor->debugfs_root, minor); |
Noralf Trønnes | e896c132 | 2018-07-03 18:03:51 +0200 | [diff] [blame] | 504 | } |
| 505 | #endif |