1 // SPDX-License-Identifier: GPL-2.0 or MIT 2 /* 3 * Copyright 2018 Noralf Trønnes 4 */ 5 6 #include <linux/export.h> 7 #include <linux/iosys-map.h> 8 #include <linux/list.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_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_gem_framebuffer_helper.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 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 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 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 if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create) 85 return -EOPNOTSUPP; 86 87 client->dev = dev; 88 client->name = name; 89 client->funcs = funcs; 90 91 ret = drm_client_modeset_create(client); 92 if (ret) 93 return ret; 94 95 ret = drm_client_open(client); 96 if (ret) 97 goto err_free; 98 99 drm_dev_get(dev); 100 101 return 0; 102 103 err_free: 104 drm_client_modeset_free(client); 105 return ret; 106 } 107 EXPORT_SYMBOL(drm_client_init); 108 109 /** 110 * drm_client_register - Register client 111 * @client: DRM client 112 * 113 * Add the client to the &drm_device client list to activate its callbacks. 114 * @client must be initialized by a call to drm_client_init(). After 115 * drm_client_register() it is no longer permissible to call drm_client_release() 116 * directly (outside the unregister callback), instead cleanup will happen 117 * automatically on driver unload. 118 * 119 * Registering a client generates a hotplug event that allows the client 120 * to set up its display from pre-existing outputs. The client must have 121 * initialized its state to able to handle the hotplug event successfully. 122 */ 123 void drm_client_register(struct drm_client_dev *client) 124 { 125 struct drm_device *dev = client->dev; 126 int ret; 127 128 mutex_lock(&dev->clientlist_mutex); 129 list_add(&client->list, &dev->clientlist); 130 131 if (client->funcs && client->funcs->hotplug) { 132 /* 133 * Perform an initial hotplug event to pick up the 134 * display configuration for the client. This step 135 * has to be performed *after* registering the client 136 * in the list of clients, or a concurrent hotplug 137 * event might be lost; leaving the display off. 138 * 139 * Hold the clientlist_mutex as for a regular hotplug 140 * event. 141 */ 142 ret = client->funcs->hotplug(client); 143 if (ret) 144 drm_dbg_kms(dev, "client hotplug ret=%d\n", ret); 145 } 146 mutex_unlock(&dev->clientlist_mutex); 147 } 148 EXPORT_SYMBOL(drm_client_register); 149 150 /** 151 * drm_client_release - Release DRM client resources 152 * @client: DRM client 153 * 154 * Releases resources by closing the &drm_file that was opened by drm_client_init(). 155 * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set. 156 * 157 * This function should only be called from the unregister callback. An exception 158 * is fbdev which cannot free the buffer if userspace has open file descriptors. 159 * 160 * Note: 161 * Clients cannot initiate a release by themselves. This is done to keep the code simple. 162 * The driver has to be unloaded before the client can be unloaded. 163 */ 164 void drm_client_release(struct drm_client_dev *client) 165 { 166 struct drm_device *dev = client->dev; 167 168 drm_dbg_kms(dev, "%s\n", client->name); 169 170 drm_client_modeset_free(client); 171 drm_client_close(client); 172 173 if (client->funcs && client->funcs->free) 174 client->funcs->free(client); 175 176 drm_dev_put(dev); 177 } 178 EXPORT_SYMBOL(drm_client_release); 179 180 /** 181 * drm_client_buffer_delete - Delete a client buffer 182 * @buffer: DRM client buffer 183 */ 184 void drm_client_buffer_delete(struct drm_client_buffer *buffer) 185 { 186 struct drm_gem_object *gem; 187 int ret; 188 189 if (!buffer) 190 return; 191 192 gem = buffer->fb->obj[0]; 193 drm_gem_vunmap(gem, &buffer->map); 194 195 ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file); 196 if (ret) 197 drm_err(buffer->client->dev, 198 "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret); 199 200 drm_gem_object_put(buffer->gem); 201 202 kfree(buffer); 203 } 204 EXPORT_SYMBOL(drm_client_buffer_delete); 205 206 static struct drm_client_buffer * 207 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, 208 u32 format, u32 handle, u32 pitch) 209 { 210 struct drm_mode_fb_cmd2 fb_req = { 211 .width = width, 212 .height = height, 213 .pixel_format = format, 214 .handles = { 215 handle, 216 }, 217 .pitches = { 218 pitch, 219 }, 220 }; 221 struct drm_device *dev = client->dev; 222 struct drm_client_buffer *buffer; 223 struct drm_gem_object *obj; 224 struct drm_framebuffer *fb; 225 int ret; 226 227 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); 228 if (!buffer) 229 return ERR_PTR(-ENOMEM); 230 231 buffer->client = client; 232 233 obj = drm_gem_object_lookup(client->file, handle); 234 if (!obj) { 235 ret = -ENOENT; 236 goto err_delete; 237 } 238 239 ret = drm_mode_addfb2(dev, &fb_req, client->file); 240 if (ret) 241 goto err_drm_gem_object_put; 242 243 fb = drm_framebuffer_lookup(dev, client->file, fb_req.fb_id); 244 if (drm_WARN_ON(dev, !fb)) { 245 ret = -ENOENT; 246 goto err_drm_mode_rmfb; 247 } 248 249 /* drop the reference we picked up in framebuffer lookup */ 250 drm_framebuffer_put(fb); 251 252 strscpy(fb->comm, client->name, TASK_COMM_LEN); 253 254 buffer->gem = obj; 255 buffer->fb = fb; 256 257 return buffer; 258 259 err_drm_mode_rmfb: 260 drm_mode_rmfb(dev, fb_req.fb_id, client->file); 261 err_drm_gem_object_put: 262 drm_gem_object_put(obj); 263 err_delete: 264 kfree(buffer); 265 return ERR_PTR(ret); 266 } 267 268 /** 269 * drm_client_buffer_vmap_local - Map DRM client buffer into address space 270 * @buffer: DRM client buffer 271 * @map_copy: Returns the mapped memory's address 272 * 273 * This function maps a client buffer into kernel address space. If the 274 * buffer is already mapped, it returns the existing mapping's address. 275 * 276 * Client buffer mappings are not ref'counted. Each call to 277 * drm_client_buffer_vmap_local() should be closely followed by a call to 278 * drm_client_buffer_vunmap_local(). See drm_client_buffer_vmap() for 279 * long-term mappings. 280 * 281 * The returned address is a copy of the internal value. In contrast to 282 * other vmap interfaces, you don't need it for the client's vunmap 283 * function. So you can modify it at will during blit and draw operations. 284 * 285 * Returns: 286 * 0 on success, or a negative errno code otherwise. 287 */ 288 int drm_client_buffer_vmap_local(struct drm_client_buffer *buffer, 289 struct iosys_map *map_copy) 290 { 291 struct drm_gem_object *gem = buffer->fb->obj[0]; 292 struct iosys_map *map = &buffer->map; 293 int ret; 294 295 drm_gem_lock(gem); 296 297 ret = drm_gem_vmap_locked(gem, map); 298 if (ret) 299 goto err_drm_gem_vmap_unlocked; 300 *map_copy = *map; 301 302 return 0; 303 304 err_drm_gem_vmap_unlocked: 305 drm_gem_unlock(gem); 306 return ret; 307 } 308 EXPORT_SYMBOL(drm_client_buffer_vmap_local); 309 310 /** 311 * drm_client_buffer_vunmap_local - Unmap DRM client buffer 312 * @buffer: DRM client buffer 313 * 314 * This function removes a client buffer's memory mapping established 315 * with drm_client_buffer_vunmap_local(). Calling this function is only 316 * required by clients that manage their buffer mappings by themselves. 317 */ 318 void drm_client_buffer_vunmap_local(struct drm_client_buffer *buffer) 319 { 320 struct drm_gem_object *gem = buffer->fb->obj[0]; 321 struct iosys_map *map = &buffer->map; 322 323 drm_gem_vunmap_locked(gem, map); 324 drm_gem_unlock(gem); 325 } 326 EXPORT_SYMBOL(drm_client_buffer_vunmap_local); 327 328 /** 329 * drm_client_buffer_vmap - Map DRM client buffer into address space 330 * @buffer: DRM client buffer 331 * @map_copy: Returns the mapped memory's address 332 * 333 * This function maps a client buffer into kernel address space. If the 334 * buffer is already mapped, it returns the existing mapping's address. 335 * 336 * Client buffer mappings are not ref'counted. Each call to 337 * drm_client_buffer_vmap() should be followed by a call to 338 * drm_client_buffer_vunmap(); or the client buffer should be mapped 339 * throughout its lifetime. 340 * 341 * The returned address is a copy of the internal value. In contrast to 342 * other vmap interfaces, you don't need it for the client's vunmap 343 * function. So you can modify it at will during blit and draw operations. 344 * 345 * Returns: 346 * 0 on success, or a negative errno code otherwise. 347 */ 348 int drm_client_buffer_vmap(struct drm_client_buffer *buffer, 349 struct iosys_map *map_copy) 350 { 351 struct drm_gem_object *gem = buffer->fb->obj[0]; 352 int ret; 353 354 ret = drm_gem_vmap(gem, &buffer->map); 355 if (ret) 356 return ret; 357 *map_copy = buffer->map; 358 359 return 0; 360 } 361 EXPORT_SYMBOL(drm_client_buffer_vmap); 362 363 /** 364 * drm_client_buffer_vunmap - Unmap DRM client buffer 365 * @buffer: DRM client buffer 366 * 367 * This function removes a client buffer's memory mapping. Calling this 368 * function is only required by clients that manage their buffer mappings 369 * by themselves. 370 */ 371 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer) 372 { 373 struct drm_gem_object *gem = buffer->fb->obj[0]; 374 375 drm_gem_vunmap(gem, &buffer->map); 376 } 377 EXPORT_SYMBOL(drm_client_buffer_vunmap); 378 379 /** 380 * drm_client_buffer_create_dumb - Create a client buffer backed by a dumb buffer 381 * @client: DRM client 382 * @width: Framebuffer width 383 * @height: Framebuffer height 384 * @format: Buffer format 385 * 386 * This function creates a &drm_client_buffer which consists of a 387 * &drm_framebuffer backed by a dumb buffer. 388 * Call drm_client_buffer_delete() to free the buffer. 389 * 390 * Returns: 391 * Pointer to a client buffer or an error pointer on failure. 392 */ 393 struct drm_client_buffer * 394 drm_client_buffer_create_dumb(struct drm_client_dev *client, u32 width, u32 height, u32 format) 395 { 396 const struct drm_format_info *info = drm_format_info(format); 397 struct drm_device *dev = client->dev; 398 struct drm_mode_create_dumb dumb_args = { }; 399 struct drm_client_buffer *buffer; 400 int ret; 401 402 dumb_args.width = width; 403 dumb_args.height = height; 404 dumb_args.bpp = drm_format_info_bpp(info, 0); 405 ret = drm_mode_create_dumb(dev, &dumb_args, client->file); 406 if (ret) 407 return ERR_PTR(ret); 408 409 buffer = drm_client_buffer_create(client, width, height, format, 410 dumb_args.handle, dumb_args.pitch); 411 if (IS_ERR(buffer)) { 412 ret = PTR_ERR(buffer); 413 goto err_drm_mode_destroy_dumb; 414 } 415 416 /* 417 * The handle is only needed for creating the framebuffer, destroy it 418 * again to solve a circular dependency should anybody export the GEM 419 * object as DMA-buf. The framebuffer and our buffer structure are still 420 * holding references to the GEM object to prevent its destruction. 421 */ 422 drm_mode_destroy_dumb(client->dev, dumb_args.handle, client->file); 423 424 return buffer; 425 426 err_drm_mode_destroy_dumb: 427 drm_mode_destroy_dumb(client->dev, dumb_args.handle, client->file); 428 return ERR_PTR(ret); 429 } 430 EXPORT_SYMBOL(drm_client_buffer_create_dumb); 431 432 /** 433 * drm_client_buffer_flush - Manually flush client buffer 434 * @buffer: DRM client buffer 435 * @rect: Damage rectangle (if NULL flushes all) 436 * 437 * This calls &drm_framebuffer_funcs->dirty (if present) to flush buffer changes 438 * for drivers that need it. 439 * 440 * Returns: 441 * Zero on success or negative error code on failure. 442 */ 443 int drm_client_buffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect) 444 { 445 if (!buffer || !buffer->fb || !buffer->fb->funcs->dirty) 446 return 0; 447 448 if (rect) { 449 struct drm_clip_rect clip = { 450 .x1 = rect->x1, 451 .y1 = rect->y1, 452 .x2 = rect->x2, 453 .y2 = rect->y2, 454 }; 455 456 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file, 457 0, 0, &clip, 1); 458 } 459 460 return buffer->fb->funcs->dirty(buffer->fb, buffer->client->file, 461 0, 0, NULL, 0); 462 } 463 EXPORT_SYMBOL(drm_client_buffer_flush); 464