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/mutex.h> 9 #include <linux/seq_file.h> 10 #include <linux/slab.h> 11 12 #include <drm/drm_client.h> 13 #include <drm/drm_device.h> 14 #include <drm/drm_drv.h> 15 #include <drm/drm_file.h> 16 #include <drm/drm_fourcc.h> 17 #include <drm/drm_framebuffer.h> 18 #include <drm/drm_gem.h> 19 #include <drm/drm_mode.h> 20 #include <drm/drm_print.h> 21 22 #include "drm_crtc_internal.h" 23 #include "drm_internal.h" 24 25 /** 26 * DOC: overview 27 * 28 * This library provides support for clients running in the kernel like fbdev and bootsplash. 29 * 30 * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported. 31 */ 32 33 static int drm_client_open(struct drm_client_dev *client) 34 { 35 struct drm_device *dev = client->dev; 36 struct drm_file *file; 37 38 file = drm_file_alloc(dev->primary); 39 if (IS_ERR(file)) 40 return PTR_ERR(file); 41 42 mutex_lock(&dev->filelist_mutex); 43 list_add(&file->lhead, &dev->filelist_internal); 44 mutex_unlock(&dev->filelist_mutex); 45 46 client->file = file; 47 48 return 0; 49 } 50 51 static void drm_client_close(struct drm_client_dev *client) 52 { 53 struct drm_device *dev = client->dev; 54 55 mutex_lock(&dev->filelist_mutex); 56 list_del(&client->file->lhead); 57 mutex_unlock(&dev->filelist_mutex); 58 59 drm_file_free(client->file); 60 } 61 62 /** 63 * drm_client_init - Initialise a DRM client 64 * @dev: DRM device 65 * @client: DRM client 66 * @name: Client name 67 * @funcs: DRM client functions (optional) 68 * 69 * This initialises the client and opens a &drm_file. 70 * Use drm_client_register() to complete the process. 71 * The caller needs to hold a reference on @dev before calling this function. 72 * The client is freed when the &drm_device is unregistered. See drm_client_release(). 73 * 74 * Returns: 75 * Zero on success or negative error code on failure. 76 */ 77 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client, 78 const char *name, const struct drm_client_funcs *funcs) 79 { 80 int ret; 81 82 if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create) 83 return -EOPNOTSUPP; 84 85 client->dev = dev; 86 client->name = name; 87 client->funcs = funcs; 88 89 ret = drm_client_modeset_create(client); 90 if (ret) 91 return ret; 92 93 ret = drm_client_open(client); 94 if (ret) 95 goto err_free; 96 97 drm_dev_get(dev); 98 99 return 0; 100 101 err_free: 102 drm_client_modeset_free(client); 103 return ret; 104 } 105 EXPORT_SYMBOL(drm_client_init); 106 107 /** 108 * drm_client_register - Register client 109 * @client: DRM client 110 * 111 * Add the client to the &drm_device client list to activate its callbacks. 112 * @client must be initialized by a call to drm_client_init(). After 113 * drm_client_register() it is no longer permissible to call drm_client_release() 114 * directly (outside the unregister callback), instead cleanup will happen 115 * automatically on driver unload. 116 * 117 * Registering a client generates a hotplug event that allows the client 118 * to set up its display from pre-existing outputs. The client must have 119 * initialized its state to able to handle the hotplug event successfully. 120 */ 121 void drm_client_register(struct drm_client_dev *client) 122 { 123 struct drm_device *dev = client->dev; 124 int ret; 125 126 mutex_lock(&dev->clientlist_mutex); 127 list_add(&client->list, &dev->clientlist); 128 129 if (client->funcs && client->funcs->hotplug) { 130 /* 131 * Perform an initial hotplug event to pick up the 132 * display configuration for the client. This step 133 * has to be performed *after* registering the client 134 * in the list of clients, or a concurrent hotplug 135 * event might be lost; leaving the display off. 136 * 137 * Hold the clientlist_mutex as for a regular hotplug 138 * event. 139 */ 140 ret = client->funcs->hotplug(client); 141 if (ret) 142 drm_dbg_kms(dev, "client hotplug ret=%d\n", ret); 143 } 144 mutex_unlock(&dev->clientlist_mutex); 145 } 146 EXPORT_SYMBOL(drm_client_register); 147 148 /** 149 * drm_client_release - Release DRM client resources 150 * @client: DRM client 151 * 152 * Releases resources by closing the &drm_file that was opened by drm_client_init(). 153 * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set. 154 * 155 * This function should only be called from the unregister callback. An exception 156 * is fbdev which cannot free the buffer if userspace has open file descriptors. 157 * 158 * Note: 159 * Clients cannot initiate a release by themselves. This is done to keep the code simple. 160 * The driver has to be unloaded before the client can be unloaded. 161 */ 162 void drm_client_release(struct drm_client_dev *client) 163 { 164 struct drm_device *dev = client->dev; 165 166 drm_dbg_kms(dev, "%s\n", client->name); 167 168 drm_client_modeset_free(client); 169 drm_client_close(client); 170 drm_dev_put(dev); 171 } 172 EXPORT_SYMBOL(drm_client_release); 173 174 static void drm_client_buffer_delete(struct drm_client_buffer *buffer) 175 { 176 if (buffer->gem) { 177 drm_gem_vunmap_unlocked(buffer->gem, &buffer->map); 178 drm_gem_object_put(buffer->gem); 179 } 180 181 kfree(buffer); 182 } 183 184 static struct drm_client_buffer * 185 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, 186 u32 format, u32 *handle) 187 { 188 const struct drm_format_info *info = drm_format_info(format); 189 struct drm_mode_create_dumb dumb_args = { }; 190 struct drm_device *dev = client->dev; 191 struct drm_client_buffer *buffer; 192 struct drm_gem_object *obj; 193 int ret; 194 195 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); 196 if (!buffer) 197 return ERR_PTR(-ENOMEM); 198 199 buffer->client = client; 200 201 dumb_args.width = width; 202 dumb_args.height = height; 203 dumb_args.bpp = drm_format_info_bpp(info, 0); 204 ret = drm_mode_create_dumb(dev, &dumb_args, client->file); 205 if (ret) 206 goto err_delete; 207 208 obj = drm_gem_object_lookup(client->file, dumb_args.handle); 209 if (!obj) { 210 ret = -ENOENT; 211 goto err_delete; 212 } 213 214 buffer->pitch = dumb_args.pitch; 215 buffer->gem = obj; 216 *handle = dumb_args.handle; 217 218 return buffer; 219 220 err_delete: 221 drm_client_buffer_delete(buffer); 222 223 return ERR_PTR(ret); 224 } 225 226 /** 227 * drm_client_buffer_vmap_local - Map DRM client buffer into address space 228 * @buffer: DRM client buffer 229 * @map_copy: Returns the mapped memory's address 230 * 231 * This function maps a client buffer into kernel address space. If the 232 * buffer is already mapped, it returns the existing mapping's address. 233 * 234 * Client buffer mappings are not ref'counted. Each call to 235 * drm_client_buffer_vmap_local() should be closely followed by a call to 236 * drm_client_buffer_vunmap_local(). See drm_client_buffer_vmap() for 237 * long-term mappings. 238 * 239 * The returned address is a copy of the internal value. In contrast to 240 * other vmap interfaces, you don't need it for the client's vunmap 241 * function. So you can modify it at will during blit and draw operations. 242 * 243 * Returns: 244 * 0 on success, or a negative errno code otherwise. 245 */ 246 int drm_client_buffer_vmap_local(struct drm_client_buffer *buffer, 247 struct iosys_map *map_copy) 248 { 249 struct drm_gem_object *gem = buffer->gem; 250 struct iosys_map *map = &buffer->map; 251 int ret; 252 253 drm_gem_lock(gem); 254 255 ret = drm_gem_vmap(gem, map); 256 if (ret) 257 goto err_drm_gem_vmap_unlocked; 258 *map_copy = *map; 259 260 return 0; 261 262 err_drm_gem_vmap_unlocked: 263 drm_gem_unlock(gem); 264 return ret; 265 } 266 EXPORT_SYMBOL(drm_client_buffer_vmap_local); 267 268 /** 269 * drm_client_buffer_vunmap_local - Unmap DRM client buffer 270 * @buffer: DRM client buffer 271 * 272 * This function removes a client buffer's memory mapping established 273 * with drm_client_buffer_vunmap_local(). Calling this function is only 274 * required by clients that manage their buffer mappings by themselves. 275 */ 276 void drm_client_buffer_vunmap_local(struct drm_client_buffer *buffer) 277 { 278 struct drm_gem_object *gem = buffer->gem; 279 struct iosys_map *map = &buffer->map; 280 281 drm_gem_vunmap(gem, map); 282 drm_gem_unlock(gem); 283 } 284 EXPORT_SYMBOL(drm_client_buffer_vunmap_local); 285 286 /** 287 * drm_client_buffer_vmap - Map DRM client buffer into address space 288 * @buffer: DRM client buffer 289 * @map_copy: Returns the mapped memory's address 290 * 291 * This function maps a client buffer into kernel address space. If the 292 * buffer is already mapped, it returns the existing mapping's address. 293 * 294 * Client buffer mappings are not ref'counted. Each call to 295 * drm_client_buffer_vmap() should be followed by a call to 296 * drm_client_buffer_vunmap(); or the client buffer should be mapped 297 * throughout its lifetime. 298 * 299 * The returned address is a copy of the internal value. In contrast to 300 * other vmap interfaces, you don't need it for the client's vunmap 301 * function. So you can modify it at will during blit and draw operations. 302 * 303 * Returns: 304 * 0 on success, or a negative errno code otherwise. 305 */ 306 int 307 drm_client_buffer_vmap(struct drm_client_buffer *buffer, 308 struct iosys_map *map_copy) 309 { 310 struct drm_gem_object *gem = buffer->gem; 311 struct iosys_map *map = &buffer->map; 312 int ret; 313 314 drm_gem_lock(gem); 315 316 ret = drm_gem_pin_locked(gem); 317 if (ret) 318 goto err_drm_gem_pin_locked; 319 ret = drm_gem_vmap(gem, map); 320 if (ret) 321 goto err_drm_gem_vmap; 322 323 drm_gem_unlock(gem); 324 325 *map_copy = *map; 326 327 return 0; 328 329 err_drm_gem_vmap: 330 drm_gem_unpin_locked(buffer->gem); 331 err_drm_gem_pin_locked: 332 drm_gem_unlock(gem); 333 return ret; 334 } 335 EXPORT_SYMBOL(drm_client_buffer_vmap); 336 337 /** 338 * drm_client_buffer_vunmap - Unmap DRM client buffer 339 * @buffer: DRM client buffer 340 * 341 * This function removes a client buffer's memory mapping. Calling this 342 * function is only required by clients that manage their buffer mappings 343 * by themselves. 344 */ 345 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer) 346 { 347 struct drm_gem_object *gem = buffer->gem; 348 struct iosys_map *map = &buffer->map; 349 350 drm_gem_lock(gem); 351 drm_gem_vunmap(gem, map); 352 drm_gem_unpin_locked(gem); 353 drm_gem_unlock(gem); 354 } 355 EXPORT_SYMBOL(drm_client_buffer_vunmap); 356 357 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer) 358 { 359 int ret; 360 361 if (!buffer->fb) 362 return; 363 364 ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file); 365 if (ret) 366 drm_err(buffer->client->dev, 367 "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret); 368 369 buffer->fb = NULL; 370 } 371 372 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer, 373 u32 width, u32 height, u32 format, 374 u32 handle) 375 { 376 struct drm_client_dev *client = buffer->client; 377 struct drm_mode_fb_cmd2 fb_req = { }; 378 int ret; 379 380 fb_req.width = width; 381 fb_req.height = height; 382 fb_req.pixel_format = format; 383 fb_req.handles[0] = handle; 384 fb_req.pitches[0] = buffer->pitch; 385 386 ret = drm_mode_addfb2(client->dev, &fb_req, client->file); 387 if (ret) 388 return ret; 389 390 buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id); 391 if (WARN_ON(!buffer->fb)) 392 return -ENOENT; 393 394 /* drop the reference we picked up in framebuffer lookup */ 395 drm_framebuffer_put(buffer->fb); 396 397 strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN); 398 399 return 0; 400 } 401 402 /** 403 * drm_client_framebuffer_create - Create a client framebuffer 404 * @client: DRM client 405 * @width: Framebuffer width 406 * @height: Framebuffer height 407 * @format: Buffer format 408 * 409 * This function creates a &drm_client_buffer which consists of a 410 * &drm_framebuffer backed by a dumb buffer. 411 * Call drm_client_framebuffer_delete() to free the buffer. 412 * 413 * Returns: 414 * Pointer to a client buffer or an error pointer on failure. 415 */ 416 struct drm_client_buffer * 417 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format) 418 { 419 struct drm_client_buffer *buffer; 420 u32 handle; 421 int ret; 422 423 buffer = drm_client_buffer_create(client, width, height, format, 424 &handle); 425 if (IS_ERR(buffer)) 426 return buffer; 427 428 ret = drm_client_buffer_addfb(buffer, width, height, format, handle); 429 430 /* 431 * The handle is only needed for creating the framebuffer, destroy it 432 * again to solve a circular dependency should anybody export the GEM 433 * object as DMA-buf. The framebuffer and our buffer structure are still 434 * holding references to the GEM object to prevent its destruction. 435 */ 436 drm_mode_destroy_dumb(client->dev, handle, client->file); 437 438 if (ret) { 439 drm_client_buffer_delete(buffer); 440 return ERR_PTR(ret); 441 } 442 443 return buffer; 444 } 445 EXPORT_SYMBOL(drm_client_framebuffer_create); 446 447 /** 448 * drm_client_framebuffer_delete - Delete a client framebuffer 449 * @buffer: DRM client buffer (can be NULL) 450 */ 451 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer) 452 { 453 if (!buffer) 454 return; 455 456 drm_client_buffer_rmfb(buffer); 457 drm_client_buffer_delete(buffer); 458 } 459 EXPORT_SYMBOL(drm_client_framebuffer_delete); 460 461 /** 462 * drm_client_framebuffer_flush - Manually flush client framebuffer 463 * @buffer: DRM client buffer (can be NULL) 464 * @rect: Damage rectangle (if NULL flushes all) 465 * 466 * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes 467 * for drivers that need it. 468 * 469 * Returns: 470 * Zero on success or negative error code on failure. 471 */ 472 int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect) 473 { 474 if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty) 475 return 0; 476 477 if (rect) { 478 struct drm_clip_rect clip = { 479 .x1 = rect->x1, 480 .y1 = rect->y1, 481 .x2 = rect->x2, 482 .y2 = rect->y2, 483 }; 484 485 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file, 486 0, 0, &clip, 1); 487 } 488 489 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file, 490 0, 0, NULL, 0); 491 } 492 EXPORT_SYMBOL(drm_client_framebuffer_flush); 493