LCOV - code coverage report
Current view: top level - drivers/gpu/drm - drm_client.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 5 159 3.1 %
Date: 2023-04-06 08:38:28 Functions: 1 17 5.9 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0 or MIT
       2             : /*
       3             :  * Copyright 2018 Noralf Trønnes
       4             :  */
       5             : 
       6             : #include <linux/iosys-map.h>
       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>
      19             : #include <drm/drm_framebuffer.h>
      20             : #include <drm/drm_gem.h>
      21             : #include <drm/drm_mode.h>
      22             : #include <drm/drm_print.h>
      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.
      31             :  *
      32             :  * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
      33             :  */
      34             : 
      35           0 : static int drm_client_open(struct drm_client_dev *client)
      36             : {
      37           0 :         struct drm_device *dev = client->dev;
      38             :         struct drm_file *file;
      39             : 
      40           0 :         file = drm_file_alloc(dev->primary);
      41           0 :         if (IS_ERR(file))
      42           0 :                 return PTR_ERR(file);
      43             : 
      44           0 :         mutex_lock(&dev->filelist_mutex);
      45           0 :         list_add(&file->lhead, &dev->filelist_internal);
      46           0 :         mutex_unlock(&dev->filelist_mutex);
      47             : 
      48           0 :         client->file = file;
      49             : 
      50             :         return 0;
      51             : }
      52             : 
      53           0 : static void drm_client_close(struct drm_client_dev *client)
      54             : {
      55           0 :         struct drm_device *dev = client->dev;
      56             : 
      57           0 :         mutex_lock(&dev->filelist_mutex);
      58           0 :         list_del(&client->file->lhead);
      59           0 :         mutex_unlock(&dev->filelist_mutex);
      60             : 
      61           0 :         drm_file_free(client->file);
      62           0 : }
      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             :  */
      79           0 : int 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           0 :         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           0 :         client->dev = dev;
      91           0 :         client->name = name;
      92           0 :         client->funcs = funcs;
      93             : 
      94           0 :         ret = drm_client_modeset_create(client);
      95           0 :         if (ret)
      96             :                 goto err_put_module;
      97             : 
      98           0 :         ret = drm_client_open(client);
      99           0 :         if (ret)
     100             :                 goto err_free;
     101             : 
     102           0 :         drm_dev_get(dev);
     103             : 
     104           0 :         return 0;
     105             : 
     106             : err_free:
     107           0 :         drm_client_modeset_free(client);
     108             : err_put_module:
     109             :         if (funcs)
     110             :                 module_put(funcs->owner);
     111             : 
     112             :         return ret;
     113             : }
     114             : EXPORT_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             :  */
     126           0 : void drm_client_register(struct drm_client_dev *client)
     127             : {
     128           0 :         struct drm_device *dev = client->dev;
     129             : 
     130           0 :         mutex_lock(&dev->clientlist_mutex);
     131           0 :         list_add(&client->list, &dev->clientlist);
     132           0 :         mutex_unlock(&dev->clientlist_mutex);
     133           0 : }
     134             : EXPORT_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             :  */
     150           0 : void drm_client_release(struct drm_client_dev *client)
     151             : {
     152           0 :         struct drm_device *dev = client->dev;
     153             : 
     154           0 :         drm_dbg_kms(dev, "%s\n", client->name);
     155             : 
     156           0 :         drm_client_modeset_free(client);
     157           0 :         drm_client_close(client);
     158           0 :         drm_dev_put(dev);
     159           0 :         if (client->funcs)
     160             :                 module_put(client->funcs->owner);
     161           0 : }
     162             : EXPORT_SYMBOL(drm_client_release);
     163             : 
     164           1 : void drm_client_dev_unregister(struct drm_device *dev)
     165             : {
     166             :         struct drm_client_dev *client, *tmp;
     167             : 
     168           1 :         if (!drm_core_check_feature(dev, DRIVER_MODESET))
     169             :                 return;
     170             : 
     171           1 :         mutex_lock(&dev->clientlist_mutex);
     172           1 :         list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
     173           0 :                 list_del(&client->list);
     174           0 :                 if (client->funcs && client->funcs->unregister) {
     175           0 :                         client->funcs->unregister(client);
     176             :                 } else {
     177           0 :                         drm_client_release(client);
     178           0 :                         kfree(client);
     179             :                 }
     180             :         }
     181           1 :         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           0 : void drm_client_dev_hotplug(struct drm_device *dev)
     194             : {
     195             :         struct drm_client_dev *client;
     196             :         int ret;
     197             : 
     198           0 :         if (!drm_core_check_feature(dev, DRIVER_MODESET))
     199             :                 return;
     200             : 
     201           0 :         if (!dev->mode_config.num_connector) {
     202           0 :                 drm_dbg_kms(dev, "No connectors found, will not send hotplug events!\n");
     203           0 :                 return;
     204             :         }
     205             : 
     206           0 :         mutex_lock(&dev->clientlist_mutex);
     207           0 :         list_for_each_entry(client, &dev->clientlist, list) {
     208           0 :                 if (!client->funcs || !client->funcs->hotplug)
     209           0 :                         continue;
     210             : 
     211           0 :                 if (client->hotplug_failed)
     212           0 :                         continue;
     213             : 
     214           0 :                 ret = client->funcs->hotplug(client);
     215           0 :                 drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
     216           0 :                 if (ret)
     217           0 :                         client->hotplug_failed = true;
     218             :         }
     219           0 :         mutex_unlock(&dev->clientlist_mutex);
     220             : }
     221             : EXPORT_SYMBOL(drm_client_dev_hotplug);
     222             : 
     223           0 : void drm_client_dev_restore(struct drm_device *dev)
     224             : {
     225             :         struct drm_client_dev *client;
     226             :         int ret;
     227             : 
     228           0 :         if (!drm_core_check_feature(dev, DRIVER_MODESET))
     229             :                 return;
     230             : 
     231           0 :         mutex_lock(&dev->clientlist_mutex);
     232           0 :         list_for_each_entry(client, &dev->clientlist, list) {
     233           0 :                 if (!client->funcs || !client->funcs->restore)
     234           0 :                         continue;
     235             : 
     236           0 :                 ret = client->funcs->restore(client);
     237           0 :                 drm_dbg_kms(dev, "%s: ret=%d\n", client->name, ret);
     238           0 :                 if (!ret) /* The first one to return zero gets the privilege to restore */
     239             :                         break;
     240             :         }
     241           0 :         mutex_unlock(&dev->clientlist_mutex);
     242             : }
     243             : 
     244           0 : static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
     245             : {
     246           0 :         if (buffer->gem) {
     247           0 :                 drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
     248           0 :                 drm_gem_object_put(buffer->gem);
     249             :         }
     250             : 
     251           0 :         kfree(buffer);
     252           0 : }
     253             : 
     254             : static struct drm_client_buffer *
     255           0 : drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
     256             :                          u32 format, u32 *handle)
     257             : {
     258           0 :         const struct drm_format_info *info = drm_format_info(format);
     259           0 :         struct drm_mode_create_dumb dumb_args = { };
     260           0 :         struct drm_device *dev = client->dev;
     261             :         struct drm_client_buffer *buffer;
     262             :         struct drm_gem_object *obj;
     263             :         int ret;
     264             : 
     265           0 :         buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
     266           0 :         if (!buffer)
     267             :                 return ERR_PTR(-ENOMEM);
     268             : 
     269           0 :         buffer->client = client;
     270             : 
     271           0 :         dumb_args.width = width;
     272           0 :         dumb_args.height = height;
     273           0 :         dumb_args.bpp = drm_format_info_bpp(info, 0);
     274           0 :         ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
     275           0 :         if (ret)
     276             :                 goto err_delete;
     277             : 
     278           0 :         obj = drm_gem_object_lookup(client->file, dumb_args.handle);
     279           0 :         if (!obj)  {
     280             :                 ret = -ENOENT;
     281             :                 goto err_delete;
     282             :         }
     283             : 
     284           0 :         buffer->pitch = dumb_args.pitch;
     285           0 :         buffer->gem = obj;
     286           0 :         *handle = dumb_args.handle;
     287             : 
     288           0 :         return buffer;
     289             : 
     290             : err_delete:
     291           0 :         drm_client_buffer_delete(buffer);
     292             : 
     293           0 :         return ERR_PTR(ret);
     294             : }
     295             : 
     296             : /**
     297             :  * drm_client_buffer_vmap - Map DRM client buffer into address space
     298             :  * @buffer: DRM client buffer
     299             :  * @map_copy: Returns the mapped memory's address
     300             :  *
     301             :  * This function maps a client buffer into kernel address space. If the
     302             :  * buffer is already mapped, it returns the existing mapping's address.
     303             :  *
     304             :  * Client buffer mappings are not ref'counted. Each call to
     305             :  * drm_client_buffer_vmap() should be followed by a call to
     306             :  * drm_client_buffer_vunmap(); or the client buffer should be mapped
     307             :  * throughout its lifetime.
     308             :  *
     309             :  * The returned address is a copy of the internal value. In contrast to
     310             :  * other vmap interfaces, you don't need it for the client's vunmap
     311             :  * function. So you can modify it at will during blit and draw operations.
     312             :  *
     313             :  * Returns:
     314             :  *      0 on success, or a negative errno code otherwise.
     315             :  */
     316             : int
     317           0 : drm_client_buffer_vmap(struct drm_client_buffer *buffer,
     318             :                        struct iosys_map *map_copy)
     319             : {
     320           0 :         struct iosys_map *map = &buffer->map;
     321             :         int ret;
     322             : 
     323             :         /*
     324             :          * FIXME: The dependency on GEM here isn't required, we could
     325             :          * convert the driver handle to a dma-buf instead and use the
     326             :          * backend-agnostic dma-buf vmap support instead. This would
     327             :          * require that the handle2fd prime ioctl is reworked to pull the
     328             :          * fd_install step out of the driver backend hooks, to make that
     329             :          * final step optional for internal users.
     330             :          */
     331           0 :         ret = drm_gem_vmap_unlocked(buffer->gem, map);
     332           0 :         if (ret)
     333             :                 return ret;
     334             : 
     335           0 :         *map_copy = *map;
     336             : 
     337           0 :         return 0;
     338             : }
     339             : EXPORT_SYMBOL(drm_client_buffer_vmap);
     340             : 
     341             : /**
     342             :  * drm_client_buffer_vunmap - Unmap DRM client buffer
     343             :  * @buffer: DRM client buffer
     344             :  *
     345             :  * This function removes a client buffer's memory mapping. Calling this
     346             :  * function is only required by clients that manage their buffer mappings
     347             :  * by themselves.
     348             :  */
     349           0 : void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
     350             : {
     351           0 :         struct iosys_map *map = &buffer->map;
     352             : 
     353           0 :         drm_gem_vunmap_unlocked(buffer->gem, map);
     354           0 : }
     355             : EXPORT_SYMBOL(drm_client_buffer_vunmap);
     356             : 
     357           0 : static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
     358             : {
     359             :         int ret;
     360             : 
     361           0 :         if (!buffer->fb)
     362             :                 return;
     363             : 
     364           0 :         ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
     365           0 :         if (ret)
     366           0 :                 drm_err(buffer->client->dev,
     367             :                         "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
     368             : 
     369           0 :         buffer->fb = NULL;
     370             : }
     371             : 
     372           0 : static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
     373             :                                    u32 width, u32 height, u32 format,
     374             :                                    u32 handle)
     375             : {
     376           0 :         struct drm_client_dev *client = buffer->client;
     377           0 :         struct drm_mode_fb_cmd fb_req = { };
     378             :         const struct drm_format_info *info;
     379             :         int ret;
     380             : 
     381           0 :         info = drm_format_info(format);
     382           0 :         fb_req.bpp = drm_format_info_bpp(info, 0);
     383           0 :         fb_req.depth = info->depth;
     384           0 :         fb_req.width = width;
     385           0 :         fb_req.height = height;
     386           0 :         fb_req.handle = handle;
     387           0 :         fb_req.pitch = buffer->pitch;
     388             : 
     389           0 :         ret = drm_mode_addfb(client->dev, &fb_req, client->file);
     390           0 :         if (ret)
     391             :                 return ret;
     392             : 
     393           0 :         buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
     394           0 :         if (WARN_ON(!buffer->fb))
     395             :                 return -ENOENT;
     396             : 
     397             :         /* drop the reference we picked up in framebuffer lookup */
     398           0 :         drm_framebuffer_put(buffer->fb);
     399             : 
     400           0 :         strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
     401             : 
     402           0 :         return 0;
     403             : }
     404             : 
     405             : /**
     406             :  * drm_client_framebuffer_create - Create a client framebuffer
     407             :  * @client: DRM client
     408             :  * @width: Framebuffer width
     409             :  * @height: Framebuffer height
     410             :  * @format: Buffer format
     411             :  *
     412             :  * This function creates a &drm_client_buffer which consists of a
     413             :  * &drm_framebuffer backed by a dumb buffer.
     414             :  * Call drm_client_framebuffer_delete() to free the buffer.
     415             :  *
     416             :  * Returns:
     417             :  * Pointer to a client buffer or an error pointer on failure.
     418             :  */
     419             : struct drm_client_buffer *
     420           0 : drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
     421             : {
     422             :         struct drm_client_buffer *buffer;
     423             :         u32 handle;
     424             :         int ret;
     425             : 
     426           0 :         buffer = drm_client_buffer_create(client, width, height, format,
     427             :                                           &handle);
     428           0 :         if (IS_ERR(buffer))
     429             :                 return buffer;
     430             : 
     431           0 :         ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
     432             : 
     433             :         /*
     434             :          * The handle is only needed for creating the framebuffer, destroy it
     435             :          * again to solve a circular dependency should anybody export the GEM
     436             :          * object as DMA-buf. The framebuffer and our buffer structure are still
     437             :          * holding references to the GEM object to prevent its destruction.
     438             :          */
     439           0 :         drm_mode_destroy_dumb(client->dev, handle, client->file);
     440             : 
     441           0 :         if (ret) {
     442           0 :                 drm_client_buffer_delete(buffer);
     443           0 :                 return ERR_PTR(ret);
     444             :         }
     445             : 
     446             :         return buffer;
     447             : }
     448             : EXPORT_SYMBOL(drm_client_framebuffer_create);
     449             : 
     450             : /**
     451             :  * drm_client_framebuffer_delete - Delete a client framebuffer
     452             :  * @buffer: DRM client buffer (can be NULL)
     453             :  */
     454           0 : void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
     455             : {
     456           0 :         if (!buffer)
     457             :                 return;
     458             : 
     459           0 :         drm_client_buffer_rmfb(buffer);
     460           0 :         drm_client_buffer_delete(buffer);
     461             : }
     462             : EXPORT_SYMBOL(drm_client_framebuffer_delete);
     463             : 
     464             : /**
     465             :  * drm_client_framebuffer_flush - Manually flush client framebuffer
     466             :  * @buffer: DRM client buffer (can be NULL)
     467             :  * @rect: Damage rectangle (if NULL flushes all)
     468             :  *
     469             :  * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes
     470             :  * for drivers that need it.
     471             :  *
     472             :  * Returns:
     473             :  * Zero on success or negative error code on failure.
     474             :  */
     475           0 : int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect)
     476             : {
     477           0 :         if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty)
     478             :                 return 0;
     479             : 
     480           0 :         if (rect) {
     481           0 :                 struct drm_clip_rect clip = {
     482           0 :                         .x1 = rect->x1,
     483           0 :                         .y1 = rect->y1,
     484           0 :                         .x2 = rect->x2,
     485           0 :                         .y2 = rect->y2,
     486             :                 };
     487             : 
     488           0 :                 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
     489             :                                                 0, 0, &clip, 1);
     490             :         }
     491             : 
     492           0 :         return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file,
     493             :                                         0, 0, NULL, 0);
     494             : }
     495             : EXPORT_SYMBOL(drm_client_framebuffer_flush);
     496             : 
     497             : #ifdef CONFIG_DEBUG_FS
     498             : static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
     499             : {
     500             :         struct drm_debugfs_entry *entry = m->private;
     501             :         struct drm_device *dev = entry->dev;
     502             :         struct drm_printer p = drm_seq_file_printer(m);
     503             :         struct drm_client_dev *client;
     504             : 
     505             :         mutex_lock(&dev->clientlist_mutex);
     506             :         list_for_each_entry(client, &dev->clientlist, list)
     507             :                 drm_printf(&p, "%s\n", client->name);
     508             :         mutex_unlock(&dev->clientlist_mutex);
     509             : 
     510             :         return 0;
     511             : }
     512             : 
     513             : static const struct drm_debugfs_info drm_client_debugfs_list[] = {
     514             :         { "internal_clients", drm_client_debugfs_internal_clients, 0 },
     515             : };
     516             : 
     517             : void drm_client_debugfs_init(struct drm_minor *minor)
     518             : {
     519             :         drm_debugfs_add_files(minor->dev, drm_client_debugfs_list,
     520             :                               ARRAY_SIZE(drm_client_debugfs_list));
     521             : }
     522             : #endif

Generated by: LCOV version 1.14