| 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/list.h> |
| 8 | #include <linux/mutex.h> |
| 9 | #include <linux/seq_file.h> |
| 10 | |
| 11 | #include <drm/drm_client.h> |
| 12 | #include <drm/drm_client_event.h> |
| 13 | #include <drm/drm_debugfs.h> |
| 14 | #include <drm/drm_device.h> |
| 15 | #include <drm/drm_drv.h> |
| 16 | #include <drm/drm_print.h> |
| 17 | |
| 18 | #include "drm_internal.h" |
| 19 | |
| 20 | /** |
| 21 | * drm_client_dev_unregister - Unregister clients |
| 22 | * @dev: DRM device |
| 23 | * |
| 24 | * This function releases all clients by calling each client's |
| 25 | * &drm_client_funcs.unregister callback. The callback function |
| 26 | * is responsibe for releaseing all resources including the client |
| 27 | * itself. |
| 28 | * |
| 29 | * The helper drm_dev_unregister() calls this function. Drivers |
| 30 | * that use it don't need to call this function themselves. |
| 31 | */ |
| 32 | void drm_client_dev_unregister(struct drm_device *dev) |
| 33 | { |
| 34 | struct drm_client_dev *client, *tmp; |
| 35 | |
| 36 | if (!drm_core_check_feature(dev, feature: DRIVER_MODESET)) |
| 37 | return; |
| 38 | |
| 39 | mutex_lock(&dev->clientlist_mutex); |
| 40 | list_for_each_entry_safe(client, tmp, &dev->clientlist, list) { |
| 41 | list_del(entry: &client->list); |
| 42 | /* |
| 43 | * Unregistering consumes and frees the client. |
| 44 | */ |
| 45 | if (client->funcs && client->funcs->unregister) |
| 46 | client->funcs->unregister(client); |
| 47 | else |
| 48 | drm_client_release(client); |
| 49 | } |
| 50 | mutex_unlock(lock: &dev->clientlist_mutex); |
| 51 | } |
| 52 | EXPORT_SYMBOL(drm_client_dev_unregister); |
| 53 | |
| 54 | static void drm_client_hotplug(struct drm_client_dev *client) |
| 55 | { |
| 56 | struct drm_device *dev = client->dev; |
| 57 | int ret; |
| 58 | |
| 59 | if (!client->funcs || !client->funcs->hotplug) |
| 60 | return; |
| 61 | |
| 62 | if (client->hotplug_failed) |
| 63 | return; |
| 64 | |
| 65 | if (client->suspended) { |
| 66 | client->hotplug_pending = true; |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | client->hotplug_pending = false; |
| 71 | ret = client->funcs->hotplug(client); |
| 72 | drm_dbg_kms(dev, "%s: ret=%d\n" , client->name, ret); |
| 73 | if (ret) |
| 74 | client->hotplug_failed = true; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * drm_client_dev_hotplug - Send hotplug event to clients |
| 79 | * @dev: DRM device |
| 80 | * |
| 81 | * This function calls the &drm_client_funcs.hotplug callback on the attached clients. |
| 82 | * |
| 83 | * drm_kms_helper_hotplug_event() calls this function, so drivers that use it |
| 84 | * don't need to call this function themselves. |
| 85 | */ |
| 86 | void drm_client_dev_hotplug(struct drm_device *dev) |
| 87 | { |
| 88 | struct drm_client_dev *client; |
| 89 | |
| 90 | if (!drm_core_check_feature(dev, feature: DRIVER_MODESET)) |
| 91 | return; |
| 92 | |
| 93 | if (!dev->mode_config.num_connector) { |
| 94 | drm_dbg_kms(dev, "No connectors found, will not send hotplug events!\n" ); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | mutex_lock(&dev->clientlist_mutex); |
| 99 | list_for_each_entry(client, &dev->clientlist, list) |
| 100 | drm_client_hotplug(client); |
| 101 | mutex_unlock(lock: &dev->clientlist_mutex); |
| 102 | } |
| 103 | EXPORT_SYMBOL(drm_client_dev_hotplug); |
| 104 | |
| 105 | void drm_client_dev_restore(struct drm_device *dev, bool force) |
| 106 | { |
| 107 | struct drm_client_dev *client; |
| 108 | int ret; |
| 109 | |
| 110 | if (!drm_core_check_feature(dev, feature: DRIVER_MODESET)) |
| 111 | return; |
| 112 | |
| 113 | mutex_lock(&dev->clientlist_mutex); |
| 114 | list_for_each_entry(client, &dev->clientlist, list) { |
| 115 | if (!client->funcs || !client->funcs->restore) |
| 116 | continue; |
| 117 | |
| 118 | ret = client->funcs->restore(client, force); |
| 119 | drm_dbg_kms(dev, "%s: ret=%d\n" , client->name, ret); |
| 120 | if (!ret) /* The first one to return zero gets the privilege to restore */ |
| 121 | break; |
| 122 | } |
| 123 | mutex_unlock(lock: &dev->clientlist_mutex); |
| 124 | } |
| 125 | |
| 126 | static int drm_client_suspend(struct drm_client_dev *client) |
| 127 | { |
| 128 | struct drm_device *dev = client->dev; |
| 129 | int ret = 0; |
| 130 | |
| 131 | if (drm_WARN_ON_ONCE(dev, client->suspended)) |
| 132 | return 0; |
| 133 | |
| 134 | if (client->funcs && client->funcs->suspend) |
| 135 | ret = client->funcs->suspend(client); |
| 136 | drm_dbg_kms(dev, "%s: ret=%d\n" , client->name, ret); |
| 137 | |
| 138 | client->suspended = true; |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | void drm_client_dev_suspend(struct drm_device *dev) |
| 144 | { |
| 145 | struct drm_client_dev *client; |
| 146 | |
| 147 | mutex_lock(&dev->clientlist_mutex); |
| 148 | list_for_each_entry(client, &dev->clientlist, list) { |
| 149 | if (!client->suspended) |
| 150 | drm_client_suspend(client); |
| 151 | } |
| 152 | mutex_unlock(lock: &dev->clientlist_mutex); |
| 153 | } |
| 154 | EXPORT_SYMBOL(drm_client_dev_suspend); |
| 155 | |
| 156 | static int drm_client_resume(struct drm_client_dev *client) |
| 157 | { |
| 158 | struct drm_device *dev = client->dev; |
| 159 | int ret = 0; |
| 160 | |
| 161 | if (drm_WARN_ON_ONCE(dev, !client->suspended)) |
| 162 | return 0; |
| 163 | |
| 164 | if (client->funcs && client->funcs->resume) |
| 165 | ret = client->funcs->resume(client); |
| 166 | drm_dbg_kms(dev, "%s: ret=%d\n" , client->name, ret); |
| 167 | |
| 168 | client->suspended = false; |
| 169 | |
| 170 | if (client->hotplug_pending) |
| 171 | drm_client_hotplug(client); |
| 172 | |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | void drm_client_dev_resume(struct drm_device *dev) |
| 177 | { |
| 178 | struct drm_client_dev *client; |
| 179 | |
| 180 | mutex_lock(&dev->clientlist_mutex); |
| 181 | list_for_each_entry(client, &dev->clientlist, list) { |
| 182 | if (client->suspended) |
| 183 | drm_client_resume(client); |
| 184 | } |
| 185 | mutex_unlock(lock: &dev->clientlist_mutex); |
| 186 | } |
| 187 | EXPORT_SYMBOL(drm_client_dev_resume); |
| 188 | |
| 189 | #ifdef CONFIG_DEBUG_FS |
| 190 | static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data) |
| 191 | { |
| 192 | struct drm_debugfs_entry *entry = m->private; |
| 193 | struct drm_device *dev = entry->dev; |
| 194 | struct drm_printer p = drm_seq_file_printer(f: m); |
| 195 | struct drm_client_dev *client; |
| 196 | |
| 197 | mutex_lock(&dev->clientlist_mutex); |
| 198 | list_for_each_entry(client, &dev->clientlist, list) |
| 199 | drm_printf(p: &p, f: "%s\n" , client->name); |
| 200 | mutex_unlock(lock: &dev->clientlist_mutex); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static const struct drm_debugfs_info drm_client_debugfs_list[] = { |
| 206 | { "internal_clients" , drm_client_debugfs_internal_clients, 0 }, |
| 207 | }; |
| 208 | |
| 209 | void drm_client_debugfs_init(struct drm_device *dev) |
| 210 | { |
| 211 | drm_debugfs_add_files(dev, files: drm_client_debugfs_list, |
| 212 | ARRAY_SIZE(drm_client_debugfs_list)); |
| 213 | } |
| 214 | #endif |
| 215 | |