hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/base/dd.c
....@@ -19,7 +19,7 @@
1919 #include <linux/debugfs.h>
2020 #include <linux/device.h>
2121 #include <linux/delay.h>
22
-#include <linux/dma-mapping.h>
22
+#include <linux/dma-map-ops.h>
2323 #include <linux/init.h>
2424 #include <linux/module.h>
2525 #include <linux/kthread.h>
....@@ -27,6 +27,7 @@
2727 #include <linux/async.h>
2828 #include <linux/pm_runtime.h>
2929 #include <linux/pinctrl/devinfo.h>
30
+#include <linux/slab.h>
3031
3132 #include "base.h"
3233 #include "power/power.h"
....@@ -96,6 +97,9 @@
9697
9798 get_device(dev);
9899
100
+ kfree(dev->p->deferred_probe_reason);
101
+ dev->p->deferred_probe_reason = NULL;
102
+
99103 /*
100104 * Drop the mutex while probing each device; the probe path may
101105 * manipulate the deferred list
....@@ -136,6 +140,8 @@
136140 if (!list_empty(&dev->p->deferred_probe)) {
137141 dev_dbg(dev, "Removed from deferred list\n");
138142 list_del_init(&dev->p->deferred_probe);
143
+ kfree(dev->p->deferred_probe_reason);
144
+ dev->p->deferred_probe_reason = NULL;
139145 }
140146 mutex_unlock(&deferred_probe_mutex);
141147 }
....@@ -179,11 +185,11 @@
179185 * Kick the re-probe thread. It may already be scheduled, but it is
180186 * safe to kick it again.
181187 */
182
- schedule_work(&deferred_probe_work);
188
+ queue_work(system_unbound_wq, &deferred_probe_work);
183189 }
184190
185191 /**
186
- * device_block_probing() - Block/defere device's probes
192
+ * device_block_probing() - Block/defer device's probes
187193 *
188194 * It will disable probing of devices and defer their probes instead.
189195 */
....@@ -206,6 +212,23 @@
206212 driver_deferred_probe_trigger();
207213 }
208214
215
+/**
216
+ * device_set_deferred_probe_reason() - Set defer probe reason message for device
217
+ * @dev: the pointer to the struct device
218
+ * @vaf: the pointer to va_format structure with message
219
+ */
220
+void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
221
+{
222
+ const char *drv = dev_driver_string(dev);
223
+
224
+ mutex_lock(&deferred_probe_mutex);
225
+
226
+ kfree(dev->p->deferred_probe_reason);
227
+ dev->p->deferred_probe_reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
228
+
229
+ mutex_unlock(&deferred_probe_mutex);
230
+}
231
+
209232 /*
210233 * deferred_devs_show() - Show the devices in the deferred probe pending list.
211234 */
....@@ -216,7 +239,8 @@
216239 mutex_lock(&deferred_probe_mutex);
217240
218241 list_for_each_entry(curr, &deferred_probe_pending_list, deferred_probe)
219
- seq_printf(s, "%s\n", dev_name(curr->device));
242
+ seq_printf(s, "%s\t%s", dev_name(curr->device),
243
+ curr->device->p->deferred_probe_reason ?: "\n");
220244
221245 mutex_unlock(&deferred_probe_mutex);
222246
....@@ -224,10 +248,15 @@
224248 }
225249 DEFINE_SHOW_ATTRIBUTE(deferred_devs);
226250
227
-static int deferred_probe_timeout = -1;
251
+int driver_deferred_probe_timeout;
252
+EXPORT_SYMBOL_GPL(driver_deferred_probe_timeout);
253
+
228254 static int __init deferred_probe_timeout_setup(char *str)
229255 {
230
- deferred_probe_timeout = simple_strtol(str, NULL, 10);
256
+ int timeout;
257
+
258
+ if (!kstrtoint(str, 10, &timeout))
259
+ driver_deferred_probe_timeout = timeout;
231260 return 1;
232261 }
233262 __setup("deferred_probe_timeout=", deferred_probe_timeout_setup);
....@@ -236,23 +265,27 @@
236265 * driver_deferred_probe_check_state() - Check deferred probe state
237266 * @dev: device to check
238267 *
239
- * Returns -ENODEV if init is done and all built-in drivers have had a chance
240
- * to probe (i.e. initcalls are done), -ETIMEDOUT if deferred probe debug
241
- * timeout has expired, or -EPROBE_DEFER if none of those conditions are met.
268
+ * Return:
269
+ * -ENODEV if initcalls have completed and modules are disabled.
270
+ * -ETIMEDOUT if the deferred probe timeout was set and has expired
271
+ * and modules are enabled.
272
+ * -EPROBE_DEFER in other cases.
242273 *
243274 * Drivers or subsystems can opt-in to calling this function instead of directly
244275 * returning -EPROBE_DEFER.
245276 */
246277 int driver_deferred_probe_check_state(struct device *dev)
247278 {
248
- if (initcalls_done) {
249
- if (!deferred_probe_timeout) {
250
- dev_WARN(dev, "deferred probe timeout, ignoring dependency");
251
- return -ETIMEDOUT;
252
- }
253
- dev_warn(dev, "ignoring dependency for device, assuming no driver");
279
+ if (!IS_ENABLED(CONFIG_MODULES) && initcalls_done) {
280
+ dev_warn(dev, "ignoring dependency for device, assuming no driver\n");
254281 return -ENODEV;
255282 }
283
+
284
+ if (!driver_deferred_probe_timeout && initcalls_done) {
285
+ dev_warn(dev, "deferred probe timeout, ignoring dependency\n");
286
+ return -ETIMEDOUT;
287
+ }
288
+
256289 return -EPROBE_DEFER;
257290 }
258291
....@@ -260,7 +293,7 @@
260293 {
261294 struct device_private *p;
262295
263
- deferred_probe_timeout = 0;
296
+ driver_deferred_probe_timeout = 0;
264297 driver_deferred_probe_trigger();
265298 flush_work(&deferred_probe_work);
266299
....@@ -296,9 +329,9 @@
296329 driver_deferred_probe_trigger();
297330 flush_work(&deferred_probe_work);
298331
299
- if (deferred_probe_timeout > 0) {
332
+ if (driver_deferred_probe_timeout > 0) {
300333 schedule_delayed_work(&deferred_probe_timeout_work,
301
- deferred_probe_timeout * HZ);
334
+ driver_deferred_probe_timeout * HZ);
302335 }
303336 return 0;
304337 }
....@@ -327,7 +360,7 @@
327360 static void driver_bound(struct device *dev)
328361 {
329362 if (device_is_bound(dev)) {
330
- printk(KERN_WARNING "%s: device %s already bound\n",
363
+ pr_warn("%s: device %s already bound\n",
331364 __func__, kobject_name(&dev->kobj));
332365 return;
333366 }
....@@ -416,10 +449,9 @@
416449 * Allow manual attachment of a driver to a device.
417450 * Caller must have already set @dev->driver.
418451 *
419
- * Note that this does not modify the bus reference count
420
- * nor take the bus's rwsem. Please verify those are accounted
421
- * for before calling this. (It is ok to call with no other effort
422
- * from a driver's probe() method.)
452
+ * Note that this does not modify the bus reference count.
453
+ * Please verify that is accounted for before calling this.
454
+ * (It is ok to call with no other effort from a driver's probe() method.)
423455 *
424456 * This function must be called with the device lock held.
425457 */
....@@ -449,6 +481,19 @@
449481 driver_deferred_probe_trigger();
450482 }
451483
484
+static ssize_t state_synced_show(struct device *dev,
485
+ struct device_attribute *attr, char *buf)
486
+{
487
+ bool val;
488
+
489
+ device_lock(dev);
490
+ val = dev->state_synced;
491
+ device_unlock(dev);
492
+
493
+ return sysfs_emit(buf, "%u\n", val);
494
+}
495
+static DEVICE_ATTR_RO(state_synced);
496
+
452497 static int really_probe(struct device *dev, struct device_driver *drv)
453498 {
454499 int ret = -EPROBE_DEFER;
....@@ -459,7 +504,7 @@
459504 if (defer_all_probes) {
460505 /*
461506 * Value of defer_all_probes can be set only by
462
- * device_defer_all_probes_enable() which, in turn, will call
507
+ * device_block_probing() which, in turn, will call
463508 * wait_for_device_probe() right after that to avoid any races.
464509 */
465510 dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
....@@ -490,13 +535,16 @@
490535 if (ret)
491536 goto pinctrl_bind_failed;
492537
493
- ret = dma_configure(dev);
494
- if (ret)
495
- goto probe_failed;
538
+ if (dev->bus->dma_configure) {
539
+ ret = dev->bus->dma_configure(dev);
540
+ if (ret)
541
+ goto probe_failed;
542
+ }
496543
497
- if (driver_sysfs_add(dev)) {
498
- printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
499
- __func__, dev_name(dev));
544
+ ret = driver_sysfs_add(dev);
545
+ if (ret) {
546
+ pr_err("%s: driver_sysfs_add(%s) failed\n",
547
+ __func__, dev_name(dev));
500548 goto probe_failed;
501549 }
502550
....@@ -516,8 +564,25 @@
516564 goto probe_failed;
517565 }
518566
567
+ ret = device_add_groups(dev, drv->dev_groups);
568
+ if (ret) {
569
+ dev_err(dev, "device_add_groups() failed\n");
570
+ goto dev_groups_failed;
571
+ }
572
+
573
+ if (dev_has_sync_state(dev)) {
574
+ ret = device_create_file(dev, &dev_attr_state_synced);
575
+ if (ret) {
576
+ dev_err(dev, "state_synced sysfs add failed\n");
577
+ goto dev_sysfs_state_synced_failed;
578
+ }
579
+ }
580
+
519581 if (test_remove) {
520582 test_remove = false;
583
+
584
+ device_remove_file(dev, &dev_attr_state_synced);
585
+ device_remove_groups(dev, drv->dev_groups);
521586
522587 if (dev->bus->remove)
523588 dev->bus->remove(dev);
....@@ -525,6 +590,9 @@
525590 drv->remove(dev);
526591
527592 devres_release_all(dev);
593
+ arch_teardown_dma_ops(dev);
594
+ kfree(dev->dma_range_map);
595
+ dev->dma_range_map = NULL;
528596 driver_sysfs_remove(dev);
529597 dev->driver = NULL;
530598 dev_set_drvdata(dev, NULL);
....@@ -546,6 +614,13 @@
546614 drv->bus->name, __func__, dev_name(dev), drv->name);
547615 goto done;
548616
617
+dev_sysfs_state_synced_failed:
618
+ device_remove_groups(dev, drv->dev_groups);
619
+dev_groups_failed:
620
+ if (dev->bus->remove)
621
+ dev->bus->remove(dev);
622
+ else if (drv->remove)
623
+ drv->remove(dev);
549624 probe_failed:
550625 if (dev->bus)
551626 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
....@@ -553,7 +628,9 @@
553628 pinctrl_bind_failed:
554629 device_links_no_driver(dev);
555630 devres_release_all(dev);
556
- dma_deconfigure(dev);
631
+ arch_teardown_dma_ops(dev);
632
+ kfree(dev->dma_range_map);
633
+ dev->dma_range_map = NULL;
557634 driver_sysfs_remove(dev);
558635 dev->driver = NULL;
559636 dev_set_drvdata(dev, NULL);
....@@ -575,9 +652,8 @@
575652 break;
576653 default:
577654 /* driver matched but the probe failed */
578
- printk(KERN_WARNING
579
- "%s: probe of %s failed with error %d\n",
580
- drv->name, dev_name(dev), ret);
655
+ pr_warn("%s: probe of %s failed with error %d\n",
656
+ drv->name, dev_name(dev), ret);
581657 }
582658 /*
583659 * Ignore errors returned by ->probe so that the next driver can try
....@@ -595,15 +671,19 @@
595671 */
596672 static int really_probe_debug(struct device *dev, struct device_driver *drv)
597673 {
598
- ktime_t calltime, delta, rettime;
674
+ ktime_t calltime, rettime;
599675 int ret;
600676
601677 calltime = ktime_get();
602678 ret = really_probe(dev, drv);
603679 rettime = ktime_get();
604
- delta = ktime_sub(rettime, calltime);
680
+ /*
681
+ * Don't change this to pr_debug() because that requires
682
+ * CONFIG_DYNAMIC_DEBUG and we want a simple 'initcall_debug' on the
683
+ * kernel commandline to print this all the time at the debug level.
684
+ */
605685 printk(KERN_DEBUG "probe of %s returned %d after %lld usecs\n",
606
- dev_name(dev), ret, (s64) ktime_to_us(delta));
686
+ dev_name(dev), ret, ktime_us_delta(rettime, calltime));
607687 return ret;
608688 }
609689
....@@ -615,9 +695,10 @@
615695 */
616696 int driver_probe_done(void)
617697 {
618
- pr_debug("%s: probe_count = %d\n", __func__,
619
- atomic_read(&probe_count));
620
- if (atomic_read(&probe_count))
698
+ int local_probe_count = atomic_read(&probe_count);
699
+
700
+ pr_debug("%s: probe_count = %d\n", __func__, local_probe_count);
701
+ if (local_probe_count)
621702 return -EBUSY;
622703 return 0;
623704 }
....@@ -687,11 +768,10 @@
687768 static int __init save_async_options(char *buf)
688769 {
689770 if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN)
690
- printk(KERN_WARNING
691
- "Too long list of driver names for 'driver_async_probe'!\n");
771
+ pr_warn("Too long list of driver names for 'driver_async_probe'!\n");
692772
693773 strlcpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN);
694
- return 0;
774
+ return 1;
695775 }
696776 __setup("driver_async_probe=", save_async_options);
697777
....@@ -762,8 +842,13 @@
762842 } else if (ret == -EPROBE_DEFER) {
763843 dev_dbg(dev, "Device match requests probe deferral\n");
764844 driver_deferred_probe_add(dev);
845
+ /*
846
+ * Device can't match with a driver right now, so don't attempt
847
+ * to match or bind with other drivers on the bus.
848
+ */
849
+ return ret;
765850 } else if (ret < 0) {
766
- dev_dbg(dev, "Bus failed to match device: %d", ret);
851
+ dev_dbg(dev, "Bus failed to match device: %d\n", ret);
767852 return ret;
768853 } /* ret > 0 means positive match */
769854
....@@ -817,6 +902,7 @@
817902 static int __device_attach(struct device *dev, bool allow_async)
818903 {
819904 int ret = 0;
905
+ bool async = false;
820906
821907 device_lock(dev);
822908 if (dev->p->dead) {
....@@ -855,7 +941,7 @@
855941 */
856942 dev_dbg(dev, "scheduling asynchronous probe\n");
857943 get_device(dev);
858
- async_schedule(__device_attach_async_helper, dev);
944
+ async = true;
859945 } else {
860946 pm_request_idle(dev);
861947 }
....@@ -865,6 +951,8 @@
865951 }
866952 out_unlock:
867953 device_unlock(dev);
954
+ if (async)
955
+ async_schedule_dev(__device_attach_async_helper, dev);
868956 return ret;
869957 }
870958
....@@ -978,6 +1066,7 @@
9781066 static int __driver_attach(struct device *dev, void *data)
9791067 {
9801068 struct device_driver *drv = data;
1069
+ bool async = false;
9811070 int ret;
9821071
9831072 /*
....@@ -997,9 +1086,18 @@
9971086 } else if (ret == -EPROBE_DEFER) {
9981087 dev_dbg(dev, "Device match requests probe deferral\n");
9991088 driver_deferred_probe_add(dev);
1089
+ /*
1090
+ * Driver could not match with device, but may match with
1091
+ * another device on the bus.
1092
+ */
1093
+ return 0;
10001094 } else if (ret < 0) {
1001
- dev_dbg(dev, "Bus failed to match device: %d", ret);
1002
- return ret;
1095
+ dev_dbg(dev, "Bus failed to match device: %d\n", ret);
1096
+ /*
1097
+ * Driver could not match with device, but may match with
1098
+ * another device on the bus.
1099
+ */
1100
+ return 0;
10031101 } /* ret > 0 means positive match */
10041102
10051103 if (driver_allows_async_probing(drv)) {
....@@ -1015,9 +1113,11 @@
10151113 if (!dev->driver) {
10161114 get_device(dev);
10171115 dev->p->async_driver = drv;
1018
- async_schedule(__driver_attach_async_helper, dev);
1116
+ async = true;
10191117 }
10201118 device_unlock(dev);
1119
+ if (async)
1120
+ async_schedule_dev(__driver_attach_async_helper, dev);
10211121 return 0;
10221122 }
10231123
....@@ -1070,8 +1170,6 @@
10701170 }
10711171 }
10721172
1073
- pm_runtime_clean_up_links(dev);
1074
-
10751173 driver_sysfs_remove(dev);
10761174
10771175 if (dev->bus)
....@@ -1081,6 +1179,9 @@
10811179
10821180 pm_runtime_put_sync(dev);
10831181
1182
+ device_remove_file(dev, &dev_attr_state_synced);
1183
+ device_remove_groups(dev, drv->dev_groups);
1184
+
10841185 if (dev->bus && dev->bus->remove)
10851186 dev->bus->remove(dev);
10861187 else if (drv->remove)
....@@ -1089,7 +1190,9 @@
10891190 device_links_driver_cleanup(dev);
10901191
10911192 devres_release_all(dev);
1092
- dma_deconfigure(dev);
1193
+ arch_teardown_dma_ops(dev);
1194
+ kfree(dev->dma_range_map);
1195
+ dev->dma_range_map = NULL;
10931196 dev->driver = NULL;
10941197 dev_set_drvdata(dev, NULL);
10951198 if (dev->pm_domain && dev->pm_domain->dismiss)
....@@ -1172,7 +1275,7 @@
11721275 spin_unlock(&drv->p->klist_devices.k_lock);
11731276 break;
11741277 }
1175
- dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
1278
+ dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
11761279 struct device_private,
11771280 knode_driver.n_node);
11781281 dev = dev_prv->device;