1df0e7f7eSEmmanuel Vadot // SPDX-License-Identifier: GPL-2.0 or MIT 2c76f0f7cSNoralf Trønnes /* 3c76f0f7cSNoralf Trønnes * Copyright 2018 Noralf Trønnes 4c76f0f7cSNoralf Trønnes */ 5c76f0f7cSNoralf Trønnes 67938f421SLucas De Marchi #include <linux/iosys-map.h> 7c76f0f7cSNoralf Trønnes #include <linux/list.h> 8c76f0f7cSNoralf Trønnes #include <linux/mutex.h> 9c76f0f7cSNoralf Trønnes #include <linux/seq_file.h> 10c76f0f7cSNoralf Trønnes #include <linux/slab.h> 11c76f0f7cSNoralf Trønnes 12c76f0f7cSNoralf Trønnes #include <drm/drm_client.h> 13c76f0f7cSNoralf Trønnes #include <drm/drm_debugfs.h> 14c76f0f7cSNoralf Trønnes #include <drm/drm_device.h> 15c76f0f7cSNoralf Trønnes #include <drm/drm_drv.h> 16c76f0f7cSNoralf Trønnes #include <drm/drm_file.h> 17c76f0f7cSNoralf Trønnes #include <drm/drm_fourcc.h> 180500c04eSSam Ravnborg #include <drm/drm_framebuffer.h> 19c76f0f7cSNoralf Trønnes #include <drm/drm_gem.h> 20c76f0f7cSNoralf Trønnes #include <drm/drm_mode.h> 21c76f0f7cSNoralf Trønnes #include <drm/drm_print.h> 22c76f0f7cSNoralf Trønnes 23c76f0f7cSNoralf Trønnes #include "drm_crtc_internal.h" 24c76f0f7cSNoralf Trønnes #include "drm_internal.h" 25c76f0f7cSNoralf Trønnes 26c76f0f7cSNoralf Trønnes /** 27c76f0f7cSNoralf Trønnes * DOC: overview 28c76f0f7cSNoralf Trønnes * 29c76f0f7cSNoralf Trønnes * This library provides support for clients running in the kernel like fbdev and bootsplash. 30c76f0f7cSNoralf Trønnes * 31c76f0f7cSNoralf Trønnes * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported. 32c76f0f7cSNoralf Trønnes */ 33c76f0f7cSNoralf Trønnes 34c76f0f7cSNoralf Trønnes static int drm_client_open(struct drm_client_dev *client) 35c76f0f7cSNoralf Trønnes { 36c76f0f7cSNoralf Trønnes struct drm_device *dev = client->dev; 37c76f0f7cSNoralf Trønnes struct drm_file *file; 38c76f0f7cSNoralf Trønnes 39c76f0f7cSNoralf Trønnes file = drm_file_alloc(dev->primary); 40c76f0f7cSNoralf Trønnes if (IS_ERR(file)) 41c76f0f7cSNoralf Trønnes return PTR_ERR(file); 42c76f0f7cSNoralf Trønnes 43c76f0f7cSNoralf Trønnes mutex_lock(&dev->filelist_mutex); 44c76f0f7cSNoralf Trønnes list_add(&file->lhead, &dev->filelist_internal); 45c76f0f7cSNoralf Trønnes mutex_unlock(&dev->filelist_mutex); 46c76f0f7cSNoralf Trønnes 47c76f0f7cSNoralf Trønnes client->file = file; 48c76f0f7cSNoralf Trønnes 49c76f0f7cSNoralf Trønnes return 0; 50c76f0f7cSNoralf Trønnes } 51c76f0f7cSNoralf Trønnes 52c76f0f7cSNoralf Trønnes static void drm_client_close(struct drm_client_dev *client) 53c76f0f7cSNoralf Trønnes { 54c76f0f7cSNoralf Trønnes struct drm_device *dev = client->dev; 55c76f0f7cSNoralf Trønnes 56c76f0f7cSNoralf Trønnes mutex_lock(&dev->filelist_mutex); 57c76f0f7cSNoralf Trønnes list_del(&client->file->lhead); 58c76f0f7cSNoralf Trønnes mutex_unlock(&dev->filelist_mutex); 59c76f0f7cSNoralf Trønnes 60c76f0f7cSNoralf Trønnes drm_file_free(client->file); 61c76f0f7cSNoralf Trønnes } 62c76f0f7cSNoralf Trønnes 63c76f0f7cSNoralf Trønnes /** 644d4c2d89SNoralf Trønnes * drm_client_init - Initialise a DRM client 65c76f0f7cSNoralf Trønnes * @dev: DRM device 66c76f0f7cSNoralf Trønnes * @client: DRM client 67c76f0f7cSNoralf Trønnes * @name: Client name 68c76f0f7cSNoralf Trønnes * @funcs: DRM client functions (optional) 69c76f0f7cSNoralf Trønnes * 70e33898a2SNoralf Trønnes * This initialises the client and opens a &drm_file. 71e33898a2SNoralf Trønnes * Use drm_client_register() to complete the process. 72c76f0f7cSNoralf Trønnes * The caller needs to hold a reference on @dev before calling this function. 73c76f0f7cSNoralf Trønnes * The client is freed when the &drm_device is unregistered. See drm_client_release(). 74c76f0f7cSNoralf Trønnes * 75c76f0f7cSNoralf Trønnes * Returns: 76c76f0f7cSNoralf Trønnes * Zero on success or negative error code on failure. 77c76f0f7cSNoralf Trønnes */ 784d4c2d89SNoralf Trønnes int drm_client_init(struct drm_device *dev, struct drm_client_dev *client, 79c76f0f7cSNoralf Trønnes const char *name, const struct drm_client_funcs *funcs) 80c76f0f7cSNoralf Trønnes { 81c76f0f7cSNoralf Trønnes int ret; 82c76f0f7cSNoralf Trønnes 83b39b5394SNoralf Trønnes if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create) 8469fdf420SChris Wilson return -EOPNOTSUPP; 85c76f0f7cSNoralf Trønnes 86c76f0f7cSNoralf Trønnes client->dev = dev; 87c76f0f7cSNoralf Trønnes client->name = name; 88c76f0f7cSNoralf Trønnes client->funcs = funcs; 89c76f0f7cSNoralf Trønnes 90d81294afSNoralf Trønnes ret = drm_client_modeset_create(client); 91c76f0f7cSNoralf Trønnes if (ret) 92312292a4SThomas Zimmermann return ret; 93c76f0f7cSNoralf Trønnes 94d81294afSNoralf Trønnes ret = drm_client_open(client); 95d81294afSNoralf Trønnes if (ret) 96d81294afSNoralf Trønnes goto err_free; 97d81294afSNoralf Trønnes 98c76f0f7cSNoralf Trønnes drm_dev_get(dev); 99c76f0f7cSNoralf Trønnes 100c76f0f7cSNoralf Trønnes return 0; 101c76f0f7cSNoralf Trønnes 102d81294afSNoralf Trønnes err_free: 103d81294afSNoralf Trønnes drm_client_modeset_free(client); 104c76f0f7cSNoralf Trønnes return ret; 105c76f0f7cSNoralf Trønnes } 1064d4c2d89SNoralf Trønnes EXPORT_SYMBOL(drm_client_init); 1074d4c2d89SNoralf Trønnes 1084d4c2d89SNoralf Trønnes /** 109e33898a2SNoralf Trønnes * drm_client_register - Register client 1104d4c2d89SNoralf Trønnes * @client: DRM client 1114d4c2d89SNoralf Trønnes * 1124d4c2d89SNoralf Trønnes * Add the client to the &drm_device client list to activate its callbacks. 1134d4c2d89SNoralf Trønnes * @client must be initialized by a call to drm_client_init(). After 114e33898a2SNoralf Trønnes * drm_client_register() it is no longer permissible to call drm_client_release() 1154d4c2d89SNoralf Trønnes * directly (outside the unregister callback), instead cleanup will happen 1164d4c2d89SNoralf Trønnes * automatically on driver unload. 11727655b9bSThomas Zimmermann * 11827655b9bSThomas Zimmermann * Registering a client generates a hotplug event that allows the client 11927655b9bSThomas Zimmermann * to set up its display from pre-existing outputs. The client must have 12027655b9bSThomas Zimmermann * initialized its state to able to handle the hotplug event successfully. 1214d4c2d89SNoralf Trønnes */ 122e33898a2SNoralf Trønnes void drm_client_register(struct drm_client_dev *client) 1234d4c2d89SNoralf Trønnes { 1244d4c2d89SNoralf Trønnes struct drm_device *dev = client->dev; 12527655b9bSThomas Zimmermann int ret; 1264d4c2d89SNoralf Trønnes 1274d4c2d89SNoralf Trønnes mutex_lock(&dev->clientlist_mutex); 1284d4c2d89SNoralf Trønnes list_add(&client->list, &dev->clientlist); 12927655b9bSThomas Zimmermann 13027655b9bSThomas Zimmermann if (client->funcs && client->funcs->hotplug) { 13127655b9bSThomas Zimmermann /* 13227655b9bSThomas Zimmermann * Perform an initial hotplug event to pick up the 13327655b9bSThomas Zimmermann * display configuration for the client. This step 13427655b9bSThomas Zimmermann * has to be performed *after* registering the client 13527655b9bSThomas Zimmermann * in the list of clients, or a concurrent hotplug 13627655b9bSThomas Zimmermann * event might be lost; leaving the display off. 13727655b9bSThomas Zimmermann * 13827655b9bSThomas Zimmermann * Hold the clientlist_mutex as for a regular hotplug 13927655b9bSThomas Zimmermann * event. 14027655b9bSThomas Zimmermann */ 14127655b9bSThomas Zimmermann ret = client->funcs->hotplug(client); 14227655b9bSThomas Zimmermann if (ret) 14327655b9bSThomas Zimmermann drm_dbg_kms(dev, "client hotplug ret=%d\n", ret); 14427655b9bSThomas Zimmermann } 1454d4c2d89SNoralf Trønnes mutex_unlock(&dev->clientlist_mutex); 1464d4c2d89SNoralf Trønnes } 147e33898a2SNoralf Trønnes EXPORT_SYMBOL(drm_client_register); 148c76f0f7cSNoralf Trønnes 149c76f0f7cSNoralf Trønnes /** 150c76f0f7cSNoralf Trønnes * drm_client_release - Release DRM client resources 151c76f0f7cSNoralf Trønnes * @client: DRM client 152c76f0f7cSNoralf Trønnes * 1534d4c2d89SNoralf Trønnes * Releases resources by closing the &drm_file that was opened by drm_client_init(). 154c76f0f7cSNoralf Trønnes * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set. 155c76f0f7cSNoralf Trønnes * 156c76f0f7cSNoralf Trønnes * This function should only be called from the unregister callback. An exception 157c76f0f7cSNoralf Trønnes * is fbdev which cannot free the buffer if userspace has open file descriptors. 158c76f0f7cSNoralf Trønnes * 159c76f0f7cSNoralf Trønnes * Note: 160c76f0f7cSNoralf Trønnes * Clients cannot initiate a release by themselves. This is done to keep the code simple. 161c76f0f7cSNoralf Trønnes * The driver has to be unloaded before the client can be unloaded. 162c76f0f7cSNoralf Trønnes */ 163c76f0f7cSNoralf Trønnes void drm_client_release(struct drm_client_dev *client) 164c76f0f7cSNoralf Trønnes { 165c76f0f7cSNoralf Trønnes struct drm_device *dev = client->dev; 166c76f0f7cSNoralf Trønnes 16741cb6603SJani Nikula drm_dbg_kms(dev, "%s\n", client->name); 168c76f0f7cSNoralf Trønnes 169d81294afSNoralf Trønnes drm_client_modeset_free(client); 170c76f0f7cSNoralf Trønnes drm_client_close(client); 171c76f0f7cSNoralf Trønnes drm_dev_put(dev); 172c76f0f7cSNoralf Trønnes } 173c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_release); 174c76f0f7cSNoralf Trønnes 175c76f0f7cSNoralf Trønnes void drm_client_dev_unregister(struct drm_device *dev) 176c76f0f7cSNoralf Trønnes { 177c76f0f7cSNoralf Trønnes struct drm_client_dev *client, *tmp; 178c76f0f7cSNoralf Trønnes 179c76f0f7cSNoralf Trønnes if (!drm_core_check_feature(dev, DRIVER_MODESET)) 180c76f0f7cSNoralf Trønnes return; 181c76f0f7cSNoralf Trønnes 182c76f0f7cSNoralf Trønnes mutex_lock(&dev->clientlist_mutex); 183c76f0f7cSNoralf Trønnes list_for_each_entry_safe(client, tmp, &dev->clientlist, list) { 184c76f0f7cSNoralf Trønnes list_del(&client->list); 185c76f0f7cSNoralf Trønnes if (client->funcs && client->funcs->unregister) { 186c76f0f7cSNoralf Trønnes client->funcs->unregister(client); 187c76f0f7cSNoralf Trønnes } else { 188c76f0f7cSNoralf Trønnes drm_client_release(client); 189c76f0f7cSNoralf Trønnes kfree(client); 190c76f0f7cSNoralf Trønnes } 191c76f0f7cSNoralf Trønnes } 192c76f0f7cSNoralf Trønnes mutex_unlock(&dev->clientlist_mutex); 193c76f0f7cSNoralf Trønnes } 194c76f0f7cSNoralf Trønnes 195c76f0f7cSNoralf Trønnes /** 196c76f0f7cSNoralf Trønnes * drm_client_dev_hotplug - Send hotplug event to clients 197c76f0f7cSNoralf Trønnes * @dev: DRM device 198c76f0f7cSNoralf Trønnes * 199c76f0f7cSNoralf Trønnes * This function calls the &drm_client_funcs.hotplug callback on the attached clients. 200c76f0f7cSNoralf Trønnes * 201c76f0f7cSNoralf Trønnes * drm_kms_helper_hotplug_event() calls this function, so drivers that use it 202c76f0f7cSNoralf Trønnes * don't need to call this function themselves. 203c76f0f7cSNoralf Trønnes */ 204c76f0f7cSNoralf Trønnes void drm_client_dev_hotplug(struct drm_device *dev) 205c76f0f7cSNoralf Trønnes { 206c76f0f7cSNoralf Trønnes struct drm_client_dev *client; 207c76f0f7cSNoralf Trønnes int ret; 208c76f0f7cSNoralf Trønnes 209c76f0f7cSNoralf Trønnes if (!drm_core_check_feature(dev, DRIVER_MODESET)) 210c76f0f7cSNoralf Trønnes return; 211c76f0f7cSNoralf Trønnes 212c2bb3be6SThomas Zimmermann if (!dev->mode_config.num_connector) { 213c2bb3be6SThomas Zimmermann drm_dbg_kms(dev, "No connectors found, will not send hotplug events!\n"); 214c2bb3be6SThomas Zimmermann return; 215c2bb3be6SThomas Zimmermann } 216c2bb3be6SThomas Zimmermann 217c76f0f7cSNoralf Trønnes mutex_lock(&dev->clientlist_mutex); 218c76f0f7cSNoralf Trønnes list_for_each_entry(client, &dev->clientlist, list) { 219c76f0f7cSNoralf Trønnes if (!client->funcs || !client->funcs->hotplug) 220c76f0f7cSNoralf Trønnes continue; 221c76f0f7cSNoralf Trønnes 2226a9d5ad3SThomas Zimmermann if (client->hotplug_failed) 2236a9d5ad3SThomas Zimmermann continue; 2246a9d5ad3SThomas Zimmermann 225c76f0f7cSNoralf Trønnes ret = client->funcs->hotplug(client); 22641cb6603SJani Nikula drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret); 2276a9d5ad3SThomas Zimmermann if (ret) 2286a9d5ad3SThomas Zimmermann client->hotplug_failed = true; 229c76f0f7cSNoralf Trønnes } 230c76f0f7cSNoralf Trønnes mutex_unlock(&dev->clientlist_mutex); 231c76f0f7cSNoralf Trønnes } 232c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_dev_hotplug); 233c76f0f7cSNoralf Trønnes 234c76f0f7cSNoralf Trønnes void drm_client_dev_restore(struct drm_device *dev) 235c76f0f7cSNoralf Trønnes { 236c76f0f7cSNoralf Trønnes struct drm_client_dev *client; 237c76f0f7cSNoralf Trønnes int ret; 238c76f0f7cSNoralf Trønnes 239c76f0f7cSNoralf Trønnes if (!drm_core_check_feature(dev, DRIVER_MODESET)) 240c76f0f7cSNoralf Trønnes return; 241c76f0f7cSNoralf Trønnes 242c76f0f7cSNoralf Trønnes mutex_lock(&dev->clientlist_mutex); 243c76f0f7cSNoralf Trønnes list_for_each_entry(client, &dev->clientlist, list) { 244c76f0f7cSNoralf Trønnes if (!client->funcs || !client->funcs->restore) 245c76f0f7cSNoralf Trønnes continue; 246c76f0f7cSNoralf Trønnes 247c76f0f7cSNoralf Trønnes ret = client->funcs->restore(client); 24841cb6603SJani Nikula drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret); 249c76f0f7cSNoralf Trønnes if (!ret) /* The first one to return zero gets the privilege to restore */ 250c76f0f7cSNoralf Trønnes break; 251c76f0f7cSNoralf Trønnes } 252c76f0f7cSNoralf Trønnes mutex_unlock(&dev->clientlist_mutex); 253c76f0f7cSNoralf Trønnes } 254c76f0f7cSNoralf Trønnes 255c76f0f7cSNoralf Trønnes static void drm_client_buffer_delete(struct drm_client_buffer *buffer) 256c76f0f7cSNoralf Trønnes { 257444bbba7SDmitry Osipenko if (buffer->gem) { 25827b2ae65SDmitry Osipenko drm_gem_vunmap_unlocked(buffer->gem, &buffer->map); 259be6ee102SEmil Velikov drm_gem_object_put(buffer->gem); 260444bbba7SDmitry Osipenko } 261c76f0f7cSNoralf Trønnes 262c76f0f7cSNoralf Trønnes kfree(buffer); 263c76f0f7cSNoralf Trønnes } 264c76f0f7cSNoralf Trønnes 265c76f0f7cSNoralf Trønnes static struct drm_client_buffer * 26685e26dd5SChristian König drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, 26785e26dd5SChristian König u32 format, u32 *handle) 268c76f0f7cSNoralf Trønnes { 26924c478eaSMaxime Ripard const struct drm_format_info *info = drm_format_info(format); 270c76f0f7cSNoralf Trønnes struct drm_mode_create_dumb dumb_args = { }; 271c76f0f7cSNoralf Trønnes struct drm_device *dev = client->dev; 272c76f0f7cSNoralf Trønnes struct drm_client_buffer *buffer; 273c76f0f7cSNoralf Trønnes struct drm_gem_object *obj; 274c76f0f7cSNoralf Trønnes int ret; 275c76f0f7cSNoralf Trønnes 276c76f0f7cSNoralf Trønnes buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); 277c76f0f7cSNoralf Trønnes if (!buffer) 278c76f0f7cSNoralf Trønnes return ERR_PTR(-ENOMEM); 279c76f0f7cSNoralf Trønnes 280c76f0f7cSNoralf Trønnes buffer->client = client; 281c76f0f7cSNoralf Trønnes 282c76f0f7cSNoralf Trønnes dumb_args.width = width; 283c76f0f7cSNoralf Trønnes dumb_args.height = height; 284356d2c8eSGeert Uytterhoeven dumb_args.bpp = drm_format_info_bpp(info, 0); 285c76f0f7cSNoralf Trønnes ret = drm_mode_create_dumb(dev, &dumb_args, client->file); 286c76f0f7cSNoralf Trønnes if (ret) 287cf19fa2cSNoralf Trønnes goto err_delete; 288c76f0f7cSNoralf Trønnes 289c76f0f7cSNoralf Trønnes obj = drm_gem_object_lookup(client->file, dumb_args.handle); 290c76f0f7cSNoralf Trønnes if (!obj) { 291c76f0f7cSNoralf Trønnes ret = -ENOENT; 292c76f0f7cSNoralf Trønnes goto err_delete; 293c76f0f7cSNoralf Trønnes } 294c76f0f7cSNoralf Trønnes 29585e26dd5SChristian König buffer->pitch = dumb_args.pitch; 296c76f0f7cSNoralf Trønnes buffer->gem = obj; 29785e26dd5SChristian König *handle = dumb_args.handle; 298c76f0f7cSNoralf Trønnes 299c76f0f7cSNoralf Trønnes return buffer; 300c76f0f7cSNoralf Trønnes 301c76f0f7cSNoralf Trønnes err_delete: 302c76f0f7cSNoralf Trønnes drm_client_buffer_delete(buffer); 303c76f0f7cSNoralf Trønnes 304c76f0f7cSNoralf Trønnes return ERR_PTR(ret); 305c76f0f7cSNoralf Trønnes } 306c76f0f7cSNoralf Trønnes 30715dd0fc8SThomas Zimmermann /** 308b4b0193eSThomas Zimmermann * drm_client_buffer_vmap_local - Map DRM client buffer into address space 309b4b0193eSThomas Zimmermann * @buffer: DRM client buffer 310b4b0193eSThomas Zimmermann * @map_copy: Returns the mapped memory's address 311b4b0193eSThomas Zimmermann * 312b4b0193eSThomas Zimmermann * This function maps a client buffer into kernel address space. If the 313b4b0193eSThomas Zimmermann * buffer is already mapped, it returns the existing mapping's address. 314b4b0193eSThomas Zimmermann * 315b4b0193eSThomas Zimmermann * Client buffer mappings are not ref'counted. Each call to 316b4b0193eSThomas Zimmermann * drm_client_buffer_vmap_local() should be closely followed by a call to 317b4b0193eSThomas Zimmermann * drm_client_buffer_vunmap_local(). See drm_client_buffer_vmap() for 318b4b0193eSThomas Zimmermann * long-term mappings. 319b4b0193eSThomas Zimmermann * 320b4b0193eSThomas Zimmermann * The returned address is a copy of the internal value. In contrast to 321b4b0193eSThomas Zimmermann * other vmap interfaces, you don't need it for the client's vunmap 322b4b0193eSThomas Zimmermann * function. So you can modify it at will during blit and draw operations. 323b4b0193eSThomas Zimmermann * 324b4b0193eSThomas Zimmermann * Returns: 325b4b0193eSThomas Zimmermann * 0 on success, or a negative errno code otherwise. 326b4b0193eSThomas Zimmermann */ 327b4b0193eSThomas Zimmermann int drm_client_buffer_vmap_local(struct drm_client_buffer *buffer, 328b4b0193eSThomas Zimmermann struct iosys_map *map_copy) 329b4b0193eSThomas Zimmermann { 330b4b0193eSThomas Zimmermann struct drm_gem_object *gem = buffer->gem; 331b4b0193eSThomas Zimmermann struct iosys_map *map = &buffer->map; 332b4b0193eSThomas Zimmermann int ret; 333b4b0193eSThomas Zimmermann 334b4b0193eSThomas Zimmermann drm_gem_lock(gem); 335b4b0193eSThomas Zimmermann 336b4b0193eSThomas Zimmermann ret = drm_gem_vmap(gem, map); 337b4b0193eSThomas Zimmermann if (ret) 338b4b0193eSThomas Zimmermann goto err_drm_gem_vmap_unlocked; 339b4b0193eSThomas Zimmermann *map_copy = *map; 340b4b0193eSThomas Zimmermann 341b4b0193eSThomas Zimmermann return 0; 342b4b0193eSThomas Zimmermann 343b4b0193eSThomas Zimmermann err_drm_gem_vmap_unlocked: 344b4b0193eSThomas Zimmermann drm_gem_unlock(gem); 345b4b0193eSThomas Zimmermann return 0; 346b4b0193eSThomas Zimmermann } 347b4b0193eSThomas Zimmermann EXPORT_SYMBOL(drm_client_buffer_vmap_local); 348b4b0193eSThomas Zimmermann 349b4b0193eSThomas Zimmermann /** 350b4b0193eSThomas Zimmermann * drm_client_buffer_vunmap_local - Unmap DRM client buffer 351b4b0193eSThomas Zimmermann * @buffer: DRM client buffer 352b4b0193eSThomas Zimmermann * 353b4b0193eSThomas Zimmermann * This function removes a client buffer's memory mapping established 354b4b0193eSThomas Zimmermann * with drm_client_buffer_vunmap_local(). Calling this function is only 355b4b0193eSThomas Zimmermann * required by clients that manage their buffer mappings by themselves. 356b4b0193eSThomas Zimmermann */ 357b4b0193eSThomas Zimmermann void drm_client_buffer_vunmap_local(struct drm_client_buffer *buffer) 358b4b0193eSThomas Zimmermann { 359b4b0193eSThomas Zimmermann struct drm_gem_object *gem = buffer->gem; 360b4b0193eSThomas Zimmermann struct iosys_map *map = &buffer->map; 361b4b0193eSThomas Zimmermann 362b4b0193eSThomas Zimmermann drm_gem_vunmap(gem, map); 363b4b0193eSThomas Zimmermann drm_gem_unlock(gem); 364b4b0193eSThomas Zimmermann } 365b4b0193eSThomas Zimmermann EXPORT_SYMBOL(drm_client_buffer_vunmap_local); 366b4b0193eSThomas Zimmermann 367b4b0193eSThomas Zimmermann /** 36815dd0fc8SThomas Zimmermann * drm_client_buffer_vmap - Map DRM client buffer into address space 36915dd0fc8SThomas Zimmermann * @buffer: DRM client buffer 370a8595556SThomas Zimmermann * @map_copy: Returns the mapped memory's address 37115dd0fc8SThomas Zimmermann * 37215dd0fc8SThomas Zimmermann * This function maps a client buffer into kernel address space. If the 373a8595556SThomas Zimmermann * buffer is already mapped, it returns the existing mapping's address. 37415dd0fc8SThomas Zimmermann * 37515dd0fc8SThomas Zimmermann * Client buffer mappings are not ref'counted. Each call to 37615dd0fc8SThomas Zimmermann * drm_client_buffer_vmap() should be followed by a call to 37715dd0fc8SThomas Zimmermann * drm_client_buffer_vunmap(); or the client buffer should be mapped 378cf1ca9aeSThomas Zimmermann * throughout its lifetime. 37915dd0fc8SThomas Zimmermann * 380a8595556SThomas Zimmermann * The returned address is a copy of the internal value. In contrast to 381a8595556SThomas Zimmermann * other vmap interfaces, you don't need it for the client's vunmap 382a8595556SThomas Zimmermann * function. So you can modify it at will during blit and draw operations. 383a8595556SThomas Zimmermann * 38415dd0fc8SThomas Zimmermann * Returns: 385a8595556SThomas Zimmermann * 0 on success, or a negative errno code otherwise. 38615dd0fc8SThomas Zimmermann */ 387a8595556SThomas Zimmermann int 3887938f421SLucas De Marchi drm_client_buffer_vmap(struct drm_client_buffer *buffer, 3897938f421SLucas De Marchi struct iosys_map *map_copy) 39015dd0fc8SThomas Zimmermann { 391*1709474bSThomas Zimmermann struct drm_gem_object *gem = buffer->gem; 3927938f421SLucas De Marchi struct iosys_map *map = &buffer->map; 393a745fb1cSThomas Zimmermann int ret; 39415dd0fc8SThomas Zimmermann 395*1709474bSThomas Zimmermann drm_gem_lock(gem); 396*1709474bSThomas Zimmermann 397*1709474bSThomas Zimmermann ret = drm_gem_pin_locked(gem); 398a745fb1cSThomas Zimmermann if (ret) 399*1709474bSThomas Zimmermann goto err_drm_gem_pin_locked; 400*1709474bSThomas Zimmermann ret = drm_gem_vmap(gem, map); 401*1709474bSThomas Zimmermann if (ret) 402*1709474bSThomas Zimmermann goto err_drm_gem_vmap; 403*1709474bSThomas Zimmermann 404*1709474bSThomas Zimmermann drm_gem_unlock(gem); 40515dd0fc8SThomas Zimmermann 406a8595556SThomas Zimmermann *map_copy = *map; 40715dd0fc8SThomas Zimmermann 408a8595556SThomas Zimmermann return 0; 409*1709474bSThomas Zimmermann 410*1709474bSThomas Zimmermann err_drm_gem_vmap: 411*1709474bSThomas Zimmermann drm_gem_unpin_locked(buffer->gem); 412*1709474bSThomas Zimmermann err_drm_gem_pin_locked: 413*1709474bSThomas Zimmermann drm_gem_unlock(gem); 414*1709474bSThomas Zimmermann return ret; 41515dd0fc8SThomas Zimmermann } 41615dd0fc8SThomas Zimmermann EXPORT_SYMBOL(drm_client_buffer_vmap); 41715dd0fc8SThomas Zimmermann 41815dd0fc8SThomas Zimmermann /** 41915dd0fc8SThomas Zimmermann * drm_client_buffer_vunmap - Unmap DRM client buffer 42015dd0fc8SThomas Zimmermann * @buffer: DRM client buffer 42115dd0fc8SThomas Zimmermann * 422cf1ca9aeSThomas Zimmermann * This function removes a client buffer's memory mapping. Calling this 423cf1ca9aeSThomas Zimmermann * function is only required by clients that manage their buffer mappings 424cf1ca9aeSThomas Zimmermann * by themselves. 42515dd0fc8SThomas Zimmermann */ 42615dd0fc8SThomas Zimmermann void drm_client_buffer_vunmap(struct drm_client_buffer *buffer) 42715dd0fc8SThomas Zimmermann { 428*1709474bSThomas Zimmermann struct drm_gem_object *gem = buffer->gem; 4297938f421SLucas De Marchi struct iosys_map *map = &buffer->map; 430a745fb1cSThomas Zimmermann 431*1709474bSThomas Zimmermann drm_gem_lock(gem); 432*1709474bSThomas Zimmermann drm_gem_vunmap(gem, map); 433*1709474bSThomas Zimmermann drm_gem_unpin_locked(gem); 434*1709474bSThomas Zimmermann drm_gem_unlock(gem); 43515dd0fc8SThomas Zimmermann } 43615dd0fc8SThomas Zimmermann EXPORT_SYMBOL(drm_client_buffer_vunmap); 43715dd0fc8SThomas Zimmermann 438c76f0f7cSNoralf Trønnes static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer) 439c76f0f7cSNoralf Trønnes { 440c76f0f7cSNoralf Trønnes int ret; 441c76f0f7cSNoralf Trønnes 442c76f0f7cSNoralf Trønnes if (!buffer->fb) 443c76f0f7cSNoralf Trønnes return; 444c76f0f7cSNoralf Trønnes 445c76f0f7cSNoralf Trønnes ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file); 446c76f0f7cSNoralf Trønnes if (ret) 44741cb6603SJani Nikula drm_err(buffer->client->dev, 448c76f0f7cSNoralf Trønnes "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret); 449c76f0f7cSNoralf Trønnes 450c76f0f7cSNoralf Trønnes buffer->fb = NULL; 451c76f0f7cSNoralf Trønnes } 452c76f0f7cSNoralf Trønnes 453c76f0f7cSNoralf Trønnes static int drm_client_buffer_addfb(struct drm_client_buffer *buffer, 45485e26dd5SChristian König u32 width, u32 height, u32 format, 45585e26dd5SChristian König u32 handle) 456c76f0f7cSNoralf Trønnes { 457c76f0f7cSNoralf Trønnes struct drm_client_dev *client = buffer->client; 4586ae2ff23SGeert Uytterhoeven struct drm_mode_fb_cmd2 fb_req = { }; 459c76f0f7cSNoralf Trønnes int ret; 460c76f0f7cSNoralf Trønnes 461c76f0f7cSNoralf Trønnes fb_req.width = width; 462c76f0f7cSNoralf Trønnes fb_req.height = height; 4636ae2ff23SGeert Uytterhoeven fb_req.pixel_format = format; 4646ae2ff23SGeert Uytterhoeven fb_req.handles[0] = handle; 4656ae2ff23SGeert Uytterhoeven fb_req.pitches[0] = buffer->pitch; 466c76f0f7cSNoralf Trønnes 4676ae2ff23SGeert Uytterhoeven ret = drm_mode_addfb2(client->dev, &fb_req, client->file); 468c76f0f7cSNoralf Trønnes if (ret) 469c76f0f7cSNoralf Trønnes return ret; 470c76f0f7cSNoralf Trønnes 471c76f0f7cSNoralf Trønnes buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id); 472c76f0f7cSNoralf Trønnes if (WARN_ON(!buffer->fb)) 473c76f0f7cSNoralf Trønnes return -ENOENT; 474c76f0f7cSNoralf Trønnes 475c76f0f7cSNoralf Trønnes /* drop the reference we picked up in framebuffer lookup */ 476c76f0f7cSNoralf Trønnes drm_framebuffer_put(buffer->fb); 477c76f0f7cSNoralf Trønnes 478c76f0f7cSNoralf Trønnes strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN); 479c76f0f7cSNoralf Trønnes 480c76f0f7cSNoralf Trønnes return 0; 481c76f0f7cSNoralf Trønnes } 482c76f0f7cSNoralf Trønnes 483c76f0f7cSNoralf Trønnes /** 484c76f0f7cSNoralf Trønnes * drm_client_framebuffer_create - Create a client framebuffer 485c76f0f7cSNoralf Trønnes * @client: DRM client 486c76f0f7cSNoralf Trønnes * @width: Framebuffer width 487c76f0f7cSNoralf Trønnes * @height: Framebuffer height 488c76f0f7cSNoralf Trønnes * @format: Buffer format 489c76f0f7cSNoralf Trønnes * 490c76f0f7cSNoralf Trønnes * This function creates a &drm_client_buffer which consists of a 491c76f0f7cSNoralf Trønnes * &drm_framebuffer backed by a dumb buffer. 492c76f0f7cSNoralf Trønnes * Call drm_client_framebuffer_delete() to free the buffer. 493c76f0f7cSNoralf Trønnes * 494c76f0f7cSNoralf Trønnes * Returns: 495c76f0f7cSNoralf Trønnes * Pointer to a client buffer or an error pointer on failure. 496c76f0f7cSNoralf Trønnes */ 497c76f0f7cSNoralf Trønnes struct drm_client_buffer * 498c76f0f7cSNoralf Trønnes drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format) 499c76f0f7cSNoralf Trønnes { 500c76f0f7cSNoralf Trønnes struct drm_client_buffer *buffer; 50185e26dd5SChristian König u32 handle; 502c76f0f7cSNoralf Trønnes int ret; 503c76f0f7cSNoralf Trønnes 50485e26dd5SChristian König buffer = drm_client_buffer_create(client, width, height, format, 50585e26dd5SChristian König &handle); 506c76f0f7cSNoralf Trønnes if (IS_ERR(buffer)) 507c76f0f7cSNoralf Trønnes return buffer; 508c76f0f7cSNoralf Trønnes 50985e26dd5SChristian König ret = drm_client_buffer_addfb(buffer, width, height, format, handle); 51085e26dd5SChristian König 51185e26dd5SChristian König /* 51285e26dd5SChristian König * The handle is only needed for creating the framebuffer, destroy it 51385e26dd5SChristian König * again to solve a circular dependency should anybody export the GEM 51485e26dd5SChristian König * object as DMA-buf. The framebuffer and our buffer structure are still 51585e26dd5SChristian König * holding references to the GEM object to prevent its destruction. 51685e26dd5SChristian König */ 51785e26dd5SChristian König drm_mode_destroy_dumb(client->dev, handle, client->file); 51885e26dd5SChristian König 519c76f0f7cSNoralf Trønnes if (ret) { 520c76f0f7cSNoralf Trønnes drm_client_buffer_delete(buffer); 521c76f0f7cSNoralf Trønnes return ERR_PTR(ret); 522c76f0f7cSNoralf Trønnes } 523c76f0f7cSNoralf Trønnes 524c76f0f7cSNoralf Trønnes return buffer; 525c76f0f7cSNoralf Trønnes } 526c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_create); 527c76f0f7cSNoralf Trønnes 528c76f0f7cSNoralf Trønnes /** 529c76f0f7cSNoralf Trønnes * drm_client_framebuffer_delete - Delete a client framebuffer 530c76f0f7cSNoralf Trønnes * @buffer: DRM client buffer (can be NULL) 531c76f0f7cSNoralf Trønnes */ 532c76f0f7cSNoralf Trønnes void drm_client_framebuffer_delete(struct drm_client_buffer *buffer) 533c76f0f7cSNoralf Trønnes { 534c76f0f7cSNoralf Trønnes if (!buffer) 535c76f0f7cSNoralf Trønnes return; 536c76f0f7cSNoralf Trønnes 537c76f0f7cSNoralf Trønnes drm_client_buffer_rmfb(buffer); 538c76f0f7cSNoralf Trønnes drm_client_buffer_delete(buffer); 539c76f0f7cSNoralf Trønnes } 540c76f0f7cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_delete); 541e896c132SNoralf Trønnes 542c9c03e3cSNoralf Trønnes /** 543c9c03e3cSNoralf Trønnes * drm_client_framebuffer_flush - Manually flush client framebuffer 544c9c03e3cSNoralf Trønnes * @buffer: DRM client buffer (can be NULL) 545c9c03e3cSNoralf Trønnes * @rect: Damage rectangle (if NULL flushes all) 546c9c03e3cSNoralf Trønnes * 547c9c03e3cSNoralf Trønnes * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes 548c9c03e3cSNoralf Trønnes * for drivers that need it. 549c9c03e3cSNoralf Trønnes * 550c9c03e3cSNoralf Trønnes * Returns: 551c9c03e3cSNoralf Trønnes * Zero on success or negative error code on failure. 552c9c03e3cSNoralf Trønnes */ 553c9c03e3cSNoralf Trønnes int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect) 554c9c03e3cSNoralf Trønnes { 555c9c03e3cSNoralf Trønnes if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty) 556c9c03e3cSNoralf Trønnes return 0; 557c9c03e3cSNoralf Trønnes 558c9c03e3cSNoralf Trønnes if (rect) { 559c9c03e3cSNoralf Trønnes struct drm_clip_rect clip = { 560c9c03e3cSNoralf Trønnes .x1 = rect->x1, 561c9c03e3cSNoralf Trønnes .y1 = rect->y1, 562c9c03e3cSNoralf Trønnes .x2 = rect->x2, 563c9c03e3cSNoralf Trønnes .y2 = rect->y2, 564c9c03e3cSNoralf Trønnes }; 565c9c03e3cSNoralf Trønnes 566c9c03e3cSNoralf Trønnes return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file, 567c9c03e3cSNoralf Trønnes 0, 0, &clip, 1); 568c9c03e3cSNoralf Trønnes } 569c9c03e3cSNoralf Trønnes 570c9c03e3cSNoralf Trønnes return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file, 571c9c03e3cSNoralf Trønnes 0, 0, NULL, 0); 572c9c03e3cSNoralf Trønnes } 573c9c03e3cSNoralf Trønnes EXPORT_SYMBOL(drm_client_framebuffer_flush); 574c9c03e3cSNoralf Trønnes 575e896c132SNoralf Trønnes #ifdef CONFIG_DEBUG_FS 576e896c132SNoralf Trønnes static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data) 577e896c132SNoralf Trønnes { 5786fd80729SMaíra Canal struct drm_debugfs_entry *entry = m->private; 5796fd80729SMaíra Canal struct drm_device *dev = entry->dev; 580e896c132SNoralf Trønnes struct drm_printer p = drm_seq_file_printer(m); 581e896c132SNoralf Trønnes struct drm_client_dev *client; 582e896c132SNoralf Trønnes 583e896c132SNoralf Trønnes mutex_lock(&dev->clientlist_mutex); 584e896c132SNoralf Trønnes list_for_each_entry(client, &dev->clientlist, list) 585e896c132SNoralf Trønnes drm_printf(&p, "%s\n", client->name); 586e896c132SNoralf Trønnes mutex_unlock(&dev->clientlist_mutex); 587e896c132SNoralf Trønnes 588e896c132SNoralf Trønnes return 0; 589e896c132SNoralf Trønnes } 590e896c132SNoralf Trønnes 5916fd80729SMaíra Canal static const struct drm_debugfs_info drm_client_debugfs_list[] = { 592e896c132SNoralf Trønnes { "internal_clients", drm_client_debugfs_internal_clients, 0 }, 593e896c132SNoralf Trønnes }; 594e896c132SNoralf Trønnes 5950b30d57aSChristian König void drm_client_debugfs_init(struct drm_device *dev) 596e896c132SNoralf Trønnes { 5970b30d57aSChristian König drm_debugfs_add_files(dev, drm_client_debugfs_list, 5986fd80729SMaíra Canal ARRAY_SIZE(drm_client_debugfs_list)); 599e896c132SNoralf Trønnes } 600e896c132SNoralf Trønnes #endif 601