linux/drivers/gpu/drm/drm_client.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright 2018 Noralf Trønnes
   4 */
   5
   6#include <linux/list.h>
   7#include <linux/module.h>
   8#include <linux/mutex.h>
   9#include <linux/seq_file.h>
  10#include <linux/slab.h>
  11
  12#include <drm/drm_client.h>
  13#include <drm/drm_debugfs.h>
  14#include <drm/drm_device.h>
  15#include <drm/drm_drv.h>
  16#include <drm/drm_file.h>
  17#include <drm/drm_fourcc.h>
  18#include <drm/drm_framebuffer.h>
  19#include <drm/drm_gem.h>
  20#include <drm/drm_mode.h>
  21#include <drm/drm_print.h>
  22
  23#include "drm_crtc_internal.h"
  24#include "drm_internal.h"
  25
  26/**
  27 * DOC: overview
  28 *
  29 * This library provides support for clients running in the kernel like fbdev and bootsplash.
  30 *
  31 * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
  32 */
  33
  34static int drm_client_open(struct drm_client_dev *client)
  35{
  36        struct drm_device *dev = client->dev;
  37        struct drm_file *file;
  38
  39        file = drm_file_alloc(dev->primary);
  40        if (IS_ERR(file))
  41                return PTR_ERR(file);
  42
  43        mutex_lock(&dev->filelist_mutex);
  44        list_add(&file->lhead, &dev->filelist_internal);
  45        mutex_unlock(&dev->filelist_mutex);
  46
  47        client->file = file;
  48
  49        return 0;
  50}
  51
  52static void drm_client_close(struct drm_client_dev *client)
  53{
  54        struct drm_device *dev = client->dev;
  55
  56        mutex_lock(&dev->filelist_mutex);
  57        list_del(&client->file->lhead);
  58        mutex_unlock(&dev->filelist_mutex);
  59
  60        drm_file_free(client->file);
  61}
  62EXPORT_SYMBOL(drm_client_close);
  63
  64/**
  65 * drm_client_init - Initialise a DRM client
  66 * @dev: DRM device
  67 * @client: DRM client
  68 * @name: Client name
  69 * @funcs: DRM client functions (optional)
  70 *
  71 * This initialises the client and opens a &drm_file.
  72 * Use drm_client_register() to complete the process.
  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 */
  79int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
  80                    const char *name, const struct drm_client_funcs *funcs)
  81{
  82        int ret;
  83
  84        if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
  85                return -EOPNOTSUPP;
  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
  94        ret = drm_client_modeset_create(client);
  95        if (ret)
  96                goto err_put_module;
  97
  98        ret = drm_client_open(client);
  99        if (ret)
 100                goto err_free;
 101
 102        drm_dev_get(dev);
 103
 104        return 0;
 105
 106err_free:
 107        drm_client_modeset_free(client);
 108err_put_module:
 109        if (funcs)
 110                module_put(funcs->owner);
 111
 112        return ret;
 113}
 114EXPORT_SYMBOL(drm_client_init);
 115
 116/**
 117 * drm_client_register - Register client
 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
 122 * drm_client_register() it is no longer permissible to call drm_client_release()
 123 * directly (outside the unregister callback), instead cleanup will happen
 124 * automatically on driver unload.
 125 */
 126void drm_client_register(struct drm_client_dev *client)
 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}
 134EXPORT_SYMBOL(drm_client_register);
 135
 136/**
 137 * drm_client_release - Release DRM client resources
 138 * @client: DRM client
 139 *
 140 * Releases resources by closing the &drm_file that was opened by drm_client_init().
 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 */
 150void drm_client_release(struct drm_client_dev *client)
 151{
 152        struct drm_device *dev = client->dev;
 153
 154        DRM_DEV_DEBUG_KMS(dev->dev, "%s\n", client->name);
 155
 156        drm_client_modeset_free(client);
 157        drm_client_close(client);
 158        drm_dev_put(dev);
 159        if (client->funcs)
 160                module_put(client->funcs->owner);
 161}
 162EXPORT_SYMBOL(drm_client_release);
 163
 164void 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 */
 193void 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);
 207                DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
 208        }
 209        mutex_unlock(&dev->clientlist_mutex);
 210}
 211EXPORT_SYMBOL(drm_client_dev_hotplug);
 212
 213void 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);
 227                DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
 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
 234static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
 235{
 236        struct drm_device *dev = buffer->client->dev;
 237
 238        drm_gem_vunmap(buffer->gem, buffer->vaddr);
 239
 240        if (buffer->gem)
 241                drm_gem_object_put_unlocked(buffer->gem);
 242
 243        if (buffer->handle)
 244                drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
 245
 246        kfree(buffer);
 247}
 248
 249static struct drm_client_buffer *
 250drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
 251{
 252        const struct drm_format_info *info = drm_format_info(format);
 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;
 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;
 267        dumb_args.bpp = info->cpp[0] * 8;
 268        ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
 269        if (ret)
 270                goto err_delete;
 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
 283        return buffer;
 284
 285err_delete:
 286        drm_client_buffer_delete(buffer);
 287
 288        return ERR_PTR(ret);
 289}
 290
 291/**
 292 * drm_client_buffer_vmap - Map DRM client buffer into address space
 293 * @buffer: DRM client buffer
 294 *
 295 * This function maps a client buffer into kernel address space. If the
 296 * buffer is already mapped, it returns the mapping's address.
 297 *
 298 * Client buffer mappings are not ref'counted. Each call to
 299 * drm_client_buffer_vmap() should be followed by a call to
 300 * drm_client_buffer_vunmap(); or the client buffer should be mapped
 301 * throughout its lifetime.
 302 *
 303 * Returns:
 304 *      The mapped memory's address
 305 */
 306void *drm_client_buffer_vmap(struct drm_client_buffer *buffer)
 307{
 308        void *vaddr;
 309
 310        if (buffer->vaddr)
 311                return buffer->vaddr;
 312
 313        /*
 314         * FIXME: The dependency on GEM here isn't required, we could
 315         * convert the driver handle to a dma-buf instead and use the
 316         * backend-agnostic dma-buf vmap support instead. This would
 317         * require that the handle2fd prime ioctl is reworked to pull the
 318         * fd_install step out of the driver backend hooks, to make that
 319         * final step optional for internal users.
 320         */
 321        vaddr = drm_gem_vmap(buffer->gem);
 322        if (IS_ERR(vaddr))
 323                return vaddr;
 324
 325        buffer->vaddr = vaddr;
 326
 327        return vaddr;
 328}
 329EXPORT_SYMBOL(drm_client_buffer_vmap);
 330
 331/**
 332 * drm_client_buffer_vunmap - Unmap DRM client buffer
 333 * @buffer: DRM client buffer
 334 *
 335 * This function removes a client buffer's memory mapping. Calling this
 336 * function is only required by clients that manage their buffer mappings
 337 * by themselves.
 338 */
 339void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
 340{
 341        drm_gem_vunmap(buffer->gem, buffer->vaddr);
 342        buffer->vaddr = NULL;
 343}
 344EXPORT_SYMBOL(drm_client_buffer_vunmap);
 345
 346static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
 347{
 348        int ret;
 349
 350        if (!buffer->fb)
 351                return;
 352
 353        ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
 354        if (ret)
 355                DRM_DEV_ERROR(buffer->client->dev->dev,
 356                              "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
 357
 358        buffer->fb = NULL;
 359}
 360
 361static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
 362                                   u32 width, u32 height, u32 format)
 363{
 364        struct drm_client_dev *client = buffer->client;
 365        struct drm_mode_fb_cmd fb_req = { };
 366        const struct drm_format_info *info;
 367        int ret;
 368
 369        info = drm_format_info(format);
 370        fb_req.bpp = info->cpp[0] * 8;
 371        fb_req.depth = info->depth;
 372        fb_req.width = width;
 373        fb_req.height = height;
 374        fb_req.handle = buffer->handle;
 375        fb_req.pitch = buffer->pitch;
 376
 377        ret = drm_mode_addfb(client->dev, &fb_req, client->file);
 378        if (ret)
 379                return ret;
 380
 381        buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
 382        if (WARN_ON(!buffer->fb))
 383                return -ENOENT;
 384
 385        /* drop the reference we picked up in framebuffer lookup */
 386        drm_framebuffer_put(buffer->fb);
 387
 388        strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
 389
 390        return 0;
 391}
 392
 393/**
 394 * drm_client_framebuffer_create - Create a client framebuffer
 395 * @client: DRM client
 396 * @width: Framebuffer width
 397 * @height: Framebuffer height
 398 * @format: Buffer format
 399 *
 400 * This function creates a &drm_client_buffer which consists of a
 401 * &drm_framebuffer backed by a dumb buffer.
 402 * Call drm_client_framebuffer_delete() to free the buffer.
 403 *
 404 * Returns:
 405 * Pointer to a client buffer or an error pointer on failure.
 406 */
 407struct drm_client_buffer *
 408drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
 409{
 410        struct drm_client_buffer *buffer;
 411        int ret;
 412
 413        buffer = drm_client_buffer_create(client, width, height, format);
 414        if (IS_ERR(buffer))
 415                return buffer;
 416
 417        ret = drm_client_buffer_addfb(buffer, width, height, format);
 418        if (ret) {
 419                drm_client_buffer_delete(buffer);
 420                return ERR_PTR(ret);
 421        }
 422
 423        return buffer;
 424}
 425EXPORT_SYMBOL(drm_client_framebuffer_create);
 426
 427/**
 428 * drm_client_framebuffer_delete - Delete a client framebuffer
 429 * @buffer: DRM client buffer (can be NULL)
 430 */
 431void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
 432{
 433        if (!buffer)
 434                return;
 435
 436        drm_client_buffer_rmfb(buffer);
 437        drm_client_buffer_delete(buffer);
 438}
 439EXPORT_SYMBOL(drm_client_framebuffer_delete);
 440
 441#ifdef CONFIG_DEBUG_FS
 442static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
 443{
 444        struct drm_info_node *node = m->private;
 445        struct drm_device *dev = node->minor->dev;
 446        struct drm_printer p = drm_seq_file_printer(m);
 447        struct drm_client_dev *client;
 448
 449        mutex_lock(&dev->clientlist_mutex);
 450        list_for_each_entry(client, &dev->clientlist, list)
 451                drm_printf(&p, "%s\n", client->name);
 452        mutex_unlock(&dev->clientlist_mutex);
 453
 454        return 0;
 455}
 456
 457static const struct drm_info_list drm_client_debugfs_list[] = {
 458        { "internal_clients", drm_client_debugfs_internal_clients, 0 },
 459};
 460
 461int drm_client_debugfs_init(struct drm_minor *minor)
 462{
 463        return drm_debugfs_create_files(drm_client_debugfs_list,
 464                                        ARRAY_SIZE(drm_client_debugfs_list),
 465                                        minor->debugfs_root, minor);
 466}
 467#endif
 468