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