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_gem.h> 19 #include <drm/drm_mode.h> 20 #include <drm/drm_print.h> 21 #include <drm/drmP.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 * Currently it's only partially implemented, just enough to support fbdev. 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 } 63 EXPORT_SYMBOL(drm_client_close); 64 65 /** 66 * drm_client_new - Create a DRM client 67 * @dev: DRM device 68 * @client: DRM client 69 * @name: Client name 70 * @funcs: DRM client functions (optional) 71 * 72 * The caller needs to hold a reference on @dev before calling this function. 73 * The client is freed when the &drm_device is unregistered. See drm_client_release(). 74 * 75 * Returns: 76 * Zero on success or negative error code on failure. 77 */ 78 int drm_client_new(struct drm_device *dev, struct drm_client_dev *client, 79 const char *name, const struct drm_client_funcs *funcs) 80 { 81 int ret; 82 83 if (!drm_core_check_feature(dev, DRIVER_MODESET) || 84 !dev->driver->dumb_create || !dev->driver->gem_prime_vmap) 85 return -ENOTSUPP; 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_open(client); 95 if (ret) 96 goto err_put_module; 97 98 mutex_lock(&dev->clientlist_mutex); 99 list_add(&client->list, &dev->clientlist); 100 mutex_unlock(&dev->clientlist_mutex); 101 102 drm_dev_get(dev); 103 104 return 0; 105 106 err_put_module: 107 if (funcs) 108 module_put(funcs->owner); 109 110 return ret; 111 } 112 EXPORT_SYMBOL(drm_client_new); 113 114 /** 115 * drm_client_release - Release DRM client resources 116 * @client: DRM client 117 * 118 * Releases resources by closing the &drm_file that was opened by drm_client_new(). 119 * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set. 120 * 121 * This function should only be called from the unregister callback. An exception 122 * is fbdev which cannot free the buffer if userspace has open file descriptors. 123 * 124 * Note: 125 * Clients cannot initiate a release by themselves. This is done to keep the code simple. 126 * The driver has to be unloaded before the client can be unloaded. 127 */ 128 void drm_client_release(struct drm_client_dev *client) 129 { 130 struct drm_device *dev = client->dev; 131 132 DRM_DEV_DEBUG_KMS(dev->dev, "%s\n", client->name); 133 134 drm_client_close(client); 135 drm_dev_put(dev); 136 if (client->funcs) 137 module_put(client->funcs->owner); 138 } 139 EXPORT_SYMBOL(drm_client_release); 140 141 void drm_client_dev_unregister(struct drm_device *dev) 142 { 143 struct drm_client_dev *client, *tmp; 144 145 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 146 return; 147 148 mutex_lock(&dev->clientlist_mutex); 149 list_for_each_entry_safe(client, tmp, &dev->clientlist, list) { 150 list_del(&client->list); 151 if (client->funcs && client->funcs->unregister) { 152 client->funcs->unregister(client); 153 } else { 154 drm_client_release(client); 155 kfree(client); 156 } 157 } 158 mutex_unlock(&dev->clientlist_mutex); 159 } 160 161 /** 162 * drm_client_dev_hotplug - Send hotplug event to clients 163 * @dev: DRM device 164 * 165 * This function calls the &drm_client_funcs.hotplug callback on the attached clients. 166 * 167 * drm_kms_helper_hotplug_event() calls this function, so drivers that use it 168 * don't need to call this function themselves. 169 */ 170 void drm_client_dev_hotplug(struct drm_device *dev) 171 { 172 struct drm_client_dev *client; 173 int ret; 174 175 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 176 return; 177 178 mutex_lock(&dev->clientlist_mutex); 179 list_for_each_entry(client, &dev->clientlist, list) { 180 if (!client->funcs || !client->funcs->hotplug) 181 continue; 182 183 ret = client->funcs->hotplug(client); 184 DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret); 185 } 186 mutex_unlock(&dev->clientlist_mutex); 187 } 188 EXPORT_SYMBOL(drm_client_dev_hotplug); 189 190 void drm_client_dev_restore(struct drm_device *dev) 191 { 192 struct drm_client_dev *client; 193 int ret; 194 195 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 196 return; 197 198 mutex_lock(&dev->clientlist_mutex); 199 list_for_each_entry(client, &dev->clientlist, list) { 200 if (!client->funcs || !client->funcs->restore) 201 continue; 202 203 ret = client->funcs->restore(client); 204 DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret); 205 if (!ret) /* The first one to return zero gets the privilege to restore */ 206 break; 207 } 208 mutex_unlock(&dev->clientlist_mutex); 209 } 210 211 static void drm_client_buffer_delete(struct drm_client_buffer *buffer) 212 { 213 struct drm_device *dev = buffer->client->dev; 214 215 if (buffer->vaddr && dev->driver->gem_prime_vunmap) 216 dev->driver->gem_prime_vunmap(buffer->gem, buffer->vaddr); 217 218 if (buffer->gem) 219 drm_gem_object_put_unlocked(buffer->gem); 220 221 if (buffer->handle) 222 drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file); 223 224 kfree(buffer); 225 } 226 227 static struct drm_client_buffer * 228 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format) 229 { 230 struct drm_mode_create_dumb dumb_args = { }; 231 struct drm_device *dev = client->dev; 232 struct drm_client_buffer *buffer; 233 struct drm_gem_object *obj; 234 void *vaddr; 235 int ret; 236 237 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); 238 if (!buffer) 239 return ERR_PTR(-ENOMEM); 240 241 buffer->client = client; 242 243 dumb_args.width = width; 244 dumb_args.height = height; 245 dumb_args.bpp = drm_format_plane_cpp(format, 0) * 8; 246 ret = drm_mode_create_dumb(dev, &dumb_args, client->file); 247 if (ret) 248 goto err_delete; 249 250 buffer->handle = dumb_args.handle; 251 buffer->pitch = dumb_args.pitch; 252 253 obj = drm_gem_object_lookup(client->file, dumb_args.handle); 254 if (!obj) { 255 ret = -ENOENT; 256 goto err_delete; 257 } 258 259 buffer->gem = obj; 260 261 /* 262 * FIXME: The dependency on GEM here isn't required, we could 263 * convert the driver handle to a dma-buf instead and use the 264 * backend-agnostic dma-buf vmap support instead. This would 265 * require that the handle2fd prime ioctl is reworked to pull the 266 * fd_install step out of the driver backend hooks, to make that 267 * final step optional for internal users. 268 */ 269 vaddr = dev->driver->gem_prime_vmap(obj); 270 if (!vaddr) { 271 ret = -ENOMEM; 272 goto err_delete; 273 } 274 275 buffer->vaddr = vaddr; 276 277 return buffer; 278 279 err_delete: 280 drm_client_buffer_delete(buffer); 281 282 return ERR_PTR(ret); 283 } 284 285 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer) 286 { 287 int ret; 288 289 if (!buffer->fb) 290 return; 291 292 ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file); 293 if (ret) 294 DRM_DEV_ERROR(buffer->client->dev->dev, 295 "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret); 296 297 buffer->fb = NULL; 298 } 299 300 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer, 301 u32 width, u32 height, u32 format) 302 { 303 struct drm_client_dev *client = buffer->client; 304 struct drm_mode_fb_cmd fb_req = { }; 305 const struct drm_format_info *info; 306 int ret; 307 308 info = drm_format_info(format); 309 fb_req.bpp = info->cpp[0] * 8; 310 fb_req.depth = info->depth; 311 fb_req.width = width; 312 fb_req.height = height; 313 fb_req.handle = buffer->handle; 314 fb_req.pitch = buffer->pitch; 315 316 ret = drm_mode_addfb(client->dev, &fb_req, client->file); 317 if (ret) 318 return ret; 319 320 buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id); 321 if (WARN_ON(!buffer->fb)) 322 return -ENOENT; 323 324 /* drop the reference we picked up in framebuffer lookup */ 325 drm_framebuffer_put(buffer->fb); 326 327 strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN); 328 329 return 0; 330 } 331 332 /** 333 * drm_client_framebuffer_create - Create a client framebuffer 334 * @client: DRM client 335 * @width: Framebuffer width 336 * @height: Framebuffer height 337 * @format: Buffer format 338 * 339 * This function creates a &drm_client_buffer which consists of a 340 * &drm_framebuffer backed by a dumb buffer. 341 * Call drm_client_framebuffer_delete() to free the buffer. 342 * 343 * Returns: 344 * Pointer to a client buffer or an error pointer on failure. 345 */ 346 struct drm_client_buffer * 347 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format) 348 { 349 struct drm_client_buffer *buffer; 350 int ret; 351 352 buffer = drm_client_buffer_create(client, width, height, format); 353 if (IS_ERR(buffer)) 354 return buffer; 355 356 ret = drm_client_buffer_addfb(buffer, width, height, format); 357 if (ret) { 358 drm_client_buffer_delete(buffer); 359 return ERR_PTR(ret); 360 } 361 362 return buffer; 363 } 364 EXPORT_SYMBOL(drm_client_framebuffer_create); 365 366 /** 367 * drm_client_framebuffer_delete - Delete a client framebuffer 368 * @buffer: DRM client buffer (can be NULL) 369 */ 370 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer) 371 { 372 if (!buffer) 373 return; 374 375 drm_client_buffer_rmfb(buffer); 376 drm_client_buffer_delete(buffer); 377 } 378 EXPORT_SYMBOL(drm_client_framebuffer_delete); 379 380 #ifdef CONFIG_DEBUG_FS 381 static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data) 382 { 383 struct drm_info_node *node = m->private; 384 struct drm_device *dev = node->minor->dev; 385 struct drm_printer p = drm_seq_file_printer(m); 386 struct drm_client_dev *client; 387 388 mutex_lock(&dev->clientlist_mutex); 389 list_for_each_entry(client, &dev->clientlist, list) 390 drm_printf(&p, "%s\n", client->name); 391 mutex_unlock(&dev->clientlist_mutex); 392 393 return 0; 394 } 395 396 static const struct drm_info_list drm_client_debugfs_list[] = { 397 { "internal_clients", drm_client_debugfs_internal_clients, 0 }, 398 }; 399 400 int drm_client_debugfs_init(struct drm_minor *minor) 401 { 402 return drm_debugfs_create_files(drm_client_debugfs_list, 403 ARRAY_SIZE(drm_client_debugfs_list), 404 minor->debugfs_root, minor); 405 } 406 #endif 407