1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * drivers/base/power/common.c - Common device power management code. 4 * 5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp. 6 */ 7 #include <linux/kernel.h> 8 #include <linux/device.h> 9 #include <linux/export.h> 10 #include <linux/slab.h> 11 #include <linux/pm_clock.h> 12 #include <linux/acpi.h> 13 #include <linux/pm_domain.h> 14 15 #include "power.h" 16 17 /** 18 * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device. 19 * @dev: Device to handle. 20 * 21 * If power.subsys_data is NULL, point it to a new object, otherwise increment 22 * its reference counter. Return 0 if new object has been created or refcount 23 * increased, otherwise negative error code. 24 */ 25 int dev_pm_get_subsys_data(struct device *dev) 26 { 27 struct pm_subsys_data *psd; 28 29 psd = kzalloc(sizeof(*psd), GFP_KERNEL); 30 if (!psd) 31 return -ENOMEM; 32 33 spin_lock_irq(&dev->power.lock); 34 35 if (dev->power.subsys_data) { 36 dev->power.subsys_data->refcount++; 37 } else { 38 spin_lock_init(&psd->lock); 39 psd->refcount = 1; 40 dev->power.subsys_data = psd; 41 pm_clk_init(dev); 42 psd = NULL; 43 } 44 45 spin_unlock_irq(&dev->power.lock); 46 47 /* kfree() verifies that its argument is nonzero. */ 48 kfree(psd); 49 50 return 0; 51 } 52 EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data); 53 54 /** 55 * dev_pm_put_subsys_data - Drop reference to power.subsys_data. 56 * @dev: Device to handle. 57 * 58 * If the reference counter of power.subsys_data is zero after dropping the 59 * reference, power.subsys_data is removed. 60 */ 61 void dev_pm_put_subsys_data(struct device *dev) 62 { 63 struct pm_subsys_data *psd; 64 65 spin_lock_irq(&dev->power.lock); 66 67 psd = dev_to_psd(dev); 68 if (!psd) 69 goto out; 70 71 if (--psd->refcount == 0) 72 dev->power.subsys_data = NULL; 73 else 74 psd = NULL; 75 76 out: 77 spin_unlock_irq(&dev->power.lock); 78 kfree(psd); 79 } 80 EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data); 81 82 /** 83 * dev_pm_domain_attach - Attach a device to its PM domain. 84 * @dev: Device to attach. 85 * @power_on: Used to indicate whether we should power on the device. 86 * 87 * The @dev may only be attached to a single PM domain. By iterating through 88 * the available alternatives we try to find a valid PM domain for the device. 89 * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain 90 * should be assigned by the corresponding attach function. 91 * 92 * This function should typically be invoked from subsystem level code during 93 * the probe phase. Especially for those that holds devices which requires 94 * power management through PM domains. 95 * 96 * Callers must ensure proper synchronization of this function with power 97 * management callbacks. 98 * 99 * Returns 0 on successfully attached PM domain, or when it is found that the 100 * device doesn't need a PM domain, else a negative error code. 101 */ 102 int dev_pm_domain_attach(struct device *dev, bool power_on) 103 { 104 int ret; 105 106 if (dev->pm_domain) 107 return 0; 108 109 ret = acpi_dev_pm_attach(dev, power_on); 110 if (!ret) 111 ret = genpd_dev_pm_attach(dev); 112 113 return ret < 0 ? ret : 0; 114 } 115 EXPORT_SYMBOL_GPL(dev_pm_domain_attach); 116 117 /** 118 * dev_pm_domain_attach_by_id - Associate a device with one of its PM domains. 119 * @dev: The device used to lookup the PM domain. 120 * @index: The index of the PM domain. 121 * 122 * As @dev may only be attached to a single PM domain, the backend PM domain 123 * provider creates a virtual device to attach instead. If attachment succeeds, 124 * the ->detach() callback in the struct dev_pm_domain are assigned by the 125 * corresponding backend attach function, as to deal with detaching of the 126 * created virtual device. 127 * 128 * This function should typically be invoked by a driver during the probe phase, 129 * in case its device requires power management through multiple PM domains. The 130 * driver may benefit from using the received device, to configure device-links 131 * towards its original device. Depending on the use-case and if needed, the 132 * links may be dynamically changed by the driver, which allows it to control 133 * the power to the PM domains independently from each other. 134 * 135 * Callers must ensure proper synchronization of this function with power 136 * management callbacks. 137 * 138 * Returns the virtual created device when successfully attached to its PM 139 * domain, NULL in case @dev don't need a PM domain, else an ERR_PTR(). 140 * Note that, to detach the returned virtual device, the driver shall call 141 * dev_pm_domain_detach() on it, typically during the remove phase. 142 */ 143 struct device *dev_pm_domain_attach_by_id(struct device *dev, 144 unsigned int index) 145 { 146 if (dev->pm_domain) 147 return ERR_PTR(-EEXIST); 148 149 return genpd_dev_pm_attach_by_id(dev, index); 150 } 151 EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id); 152 153 /** 154 * dev_pm_domain_attach_by_name - Associate a device with one of its PM domains. 155 * @dev: The device used to lookup the PM domain. 156 * @name: The name of the PM domain. 157 * 158 * For a detailed function description, see dev_pm_domain_attach_by_id(). 159 */ 160 struct device *dev_pm_domain_attach_by_name(struct device *dev, 161 const char *name) 162 { 163 if (dev->pm_domain) 164 return ERR_PTR(-EEXIST); 165 166 return genpd_dev_pm_attach_by_name(dev, name); 167 } 168 EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_name); 169 170 /** 171 * dev_pm_domain_attach_list - Associate a device with its PM domains. 172 * @dev: The device used to lookup the PM domains for. 173 * @data: The data used for attaching to the PM domains. 174 * @list: An out-parameter with an allocated list of attached PM domains. 175 * 176 * This function helps to attach a device to its multiple PM domains. The 177 * caller, which is typically a driver's probe function, may provide a list of 178 * names for the PM domains that we should try to attach the device to, but it 179 * may also provide an empty list, in case the attach should be done for all of 180 * the available PM domains. 181 * 182 * Callers must ensure proper synchronization of this function with power 183 * management callbacks. 184 * 185 * Returns the number of attached PM domains or a negative error code in case of 186 * a failure. Note that, to detach the list of PM domains, the driver shall call 187 * dev_pm_domain_detach_list(), typically during the remove phase. 188 */ 189 int dev_pm_domain_attach_list(struct device *dev, 190 const struct dev_pm_domain_attach_data *data, 191 struct dev_pm_domain_list **list) 192 { 193 struct device_node *np = dev->of_node; 194 struct dev_pm_domain_list *pds; 195 struct device *pd_dev = NULL; 196 int ret, i, num_pds = 0; 197 bool by_id = true; 198 size_t size; 199 u32 pd_flags = data ? data->pd_flags : 0; 200 u32 link_flags = pd_flags & PD_FLAG_NO_DEV_LINK ? 0 : 201 DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME; 202 203 if (dev->pm_domain) 204 return -EEXIST; 205 206 /* For now this is limited to OF based platforms. */ 207 if (!np) 208 return 0; 209 210 if (data && data->pd_names) { 211 num_pds = data->num_pd_names; 212 by_id = false; 213 } else { 214 num_pds = of_count_phandle_with_args(np, "power-domains", 215 "#power-domain-cells"); 216 } 217 218 if (num_pds <= 0) 219 return 0; 220 221 pds = kzalloc(sizeof(*pds), GFP_KERNEL); 222 if (!pds) 223 return -ENOMEM; 224 225 size = sizeof(*pds->pd_devs) + sizeof(*pds->pd_links); 226 pds->pd_devs = kcalloc(num_pds, size, GFP_KERNEL); 227 if (!pds->pd_devs) { 228 ret = -ENOMEM; 229 goto free_pds; 230 } 231 pds->pd_links = (void *)(pds->pd_devs + num_pds); 232 233 if (link_flags && pd_flags & PD_FLAG_DEV_LINK_ON) 234 link_flags |= DL_FLAG_RPM_ACTIVE; 235 236 for (i = 0; i < num_pds; i++) { 237 if (by_id) 238 pd_dev = dev_pm_domain_attach_by_id(dev, i); 239 else 240 pd_dev = dev_pm_domain_attach_by_name(dev, 241 data->pd_names[i]); 242 if (IS_ERR_OR_NULL(pd_dev)) { 243 ret = pd_dev ? PTR_ERR(pd_dev) : -ENODEV; 244 goto err_attach; 245 } 246 247 if (link_flags) { 248 struct device_link *link; 249 250 link = device_link_add(dev, pd_dev, link_flags); 251 if (!link) { 252 ret = -ENODEV; 253 goto err_link; 254 } 255 256 pds->pd_links[i] = link; 257 } 258 259 pds->pd_devs[i] = pd_dev; 260 } 261 262 pds->num_pds = num_pds; 263 *list = pds; 264 return num_pds; 265 266 err_link: 267 dev_pm_domain_detach(pd_dev, true); 268 err_attach: 269 while (--i >= 0) { 270 if (pds->pd_links[i]) 271 device_link_del(pds->pd_links[i]); 272 dev_pm_domain_detach(pds->pd_devs[i], true); 273 } 274 kfree(pds->pd_devs); 275 free_pds: 276 kfree(pds); 277 return ret; 278 } 279 EXPORT_SYMBOL_GPL(dev_pm_domain_attach_list); 280 281 /** 282 * devm_pm_domain_detach_list - devres-enabled version of dev_pm_domain_detach_list. 283 * @_list: The list of PM domains to detach. 284 * 285 * This function reverse the actions from devm_pm_domain_attach_list(). 286 * it will be invoked during the remove phase from drivers implicitly if driver 287 * uses devm_pm_domain_attach_list() to attach the PM domains. 288 */ 289 static void devm_pm_domain_detach_list(void *_list) 290 { 291 struct dev_pm_domain_list *list = _list; 292 293 dev_pm_domain_detach_list(list); 294 } 295 296 /** 297 * devm_pm_domain_attach_list - devres-enabled version of dev_pm_domain_attach_list 298 * @dev: The device used to lookup the PM domains for. 299 * @data: The data used for attaching to the PM domains. 300 * @list: An out-parameter with an allocated list of attached PM domains. 301 * 302 * NOTE: this will also handle calling devm_pm_domain_detach_list() for 303 * you during remove phase. 304 * 305 * Returns the number of attached PM domains or a negative error code in case of 306 * a failure. 307 */ 308 int devm_pm_domain_attach_list(struct device *dev, 309 const struct dev_pm_domain_attach_data *data, 310 struct dev_pm_domain_list **list) 311 { 312 int ret, num_pds; 313 314 num_pds = dev_pm_domain_attach_list(dev, data, list); 315 if (num_pds <= 0) 316 return num_pds; 317 318 ret = devm_add_action_or_reset(dev, devm_pm_domain_detach_list, *list); 319 if (ret) 320 return ret; 321 322 return num_pds; 323 } 324 EXPORT_SYMBOL_GPL(devm_pm_domain_attach_list); 325 326 /** 327 * dev_pm_domain_detach - Detach a device from its PM domain. 328 * @dev: Device to detach. 329 * @power_off: Used to indicate whether we should power off the device. 330 * 331 * This functions will reverse the actions from dev_pm_domain_attach(), 332 * dev_pm_domain_attach_by_id() and dev_pm_domain_attach_by_name(), thus it 333 * detaches @dev from its PM domain. Typically it should be invoked during the 334 * remove phase, either from subsystem level code or from drivers. 335 * 336 * Callers must ensure proper synchronization of this function with power 337 * management callbacks. 338 */ 339 void dev_pm_domain_detach(struct device *dev, bool power_off) 340 { 341 if (dev->pm_domain && dev->pm_domain->detach) 342 dev->pm_domain->detach(dev, power_off); 343 } 344 EXPORT_SYMBOL_GPL(dev_pm_domain_detach); 345 346 /** 347 * dev_pm_domain_detach_list - Detach a list of PM domains. 348 * @list: The list of PM domains to detach. 349 * 350 * This function reverse the actions from dev_pm_domain_attach_list(). 351 * Typically it should be invoked during the remove phase from drivers. 352 * 353 * Callers must ensure proper synchronization of this function with power 354 * management callbacks. 355 */ 356 void dev_pm_domain_detach_list(struct dev_pm_domain_list *list) 357 { 358 int i; 359 360 if (!list) 361 return; 362 363 for (i = 0; i < list->num_pds; i++) { 364 if (list->pd_links[i]) 365 device_link_del(list->pd_links[i]); 366 dev_pm_domain_detach(list->pd_devs[i], true); 367 } 368 369 kfree(list->pd_devs); 370 kfree(list); 371 } 372 EXPORT_SYMBOL_GPL(dev_pm_domain_detach_list); 373 374 /** 375 * dev_pm_domain_start - Start the device through its PM domain. 376 * @dev: Device to start. 377 * 378 * This function should typically be called during probe by a subsystem/driver, 379 * when it needs to start its device from the PM domain's perspective. Note 380 * that, it's assumed that the PM domain is already powered on when this 381 * function is called. 382 * 383 * Returns 0 on success and negative error values on failures. 384 */ 385 int dev_pm_domain_start(struct device *dev) 386 { 387 if (dev->pm_domain && dev->pm_domain->start) 388 return dev->pm_domain->start(dev); 389 390 return 0; 391 } 392 EXPORT_SYMBOL_GPL(dev_pm_domain_start); 393 394 /** 395 * dev_pm_domain_set - Set PM domain of a device. 396 * @dev: Device whose PM domain is to be set. 397 * @pd: PM domain to be set, or NULL. 398 * 399 * Sets the PM domain the device belongs to. The PM domain of a device needs 400 * to be set before its probe finishes (it's bound to a driver). 401 * 402 * This function must be called with the device lock held. 403 */ 404 void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd) 405 { 406 if (dev->pm_domain == pd) 407 return; 408 409 WARN(pd && device_is_bound(dev), 410 "PM domains can only be changed for unbound devices\n"); 411 dev->pm_domain = pd; 412 device_pm_check_callbacks(dev); 413 } 414 EXPORT_SYMBOL_GPL(dev_pm_domain_set); 415 416 /** 417 * dev_pm_domain_set_performance_state - Request a new performance state. 418 * @dev: The device to make the request for. 419 * @state: Target performance state for the device. 420 * 421 * This function should be called when a new performance state needs to be 422 * requested for a device that is attached to a PM domain. Note that, the 423 * support for performance scaling for PM domains is optional. 424 * 425 * Returns 0 on success and when performance scaling isn't supported, negative 426 * error code on failure. 427 */ 428 int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state) 429 { 430 if (dev->pm_domain && dev->pm_domain->set_performance_state) 431 return dev->pm_domain->set_performance_state(dev, state); 432 433 return 0; 434 } 435 EXPORT_SYMBOL_GPL(dev_pm_domain_set_performance_state); 436