Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / firmware / raspberrypi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Defines interfaces for interacting with the Raspberry Pi firmware's
4  * property channel.
5  *
6  * Copyright © 2015 Broadcom
7  */
8
9 #include <linux/dma-mapping.h>
10 #include <linux/kref.h>
11 #include <linux/mailbox_client.h>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/reboot.h>
16 #include <linux/slab.h>
17 #include <soc/bcm2835/raspberrypi-firmware.h>
18
19 #define MBOX_MSG(chan, data28)          (((data28) & ~0xf) | ((chan) & 0xf))
20 #define MBOX_CHAN(msg)                  ((msg) & 0xf)
21 #define MBOX_DATA28(msg)                ((msg) & ~0xf)
22 #define MBOX_CHAN_PROPERTY              8
23
24 static struct platform_device *rpi_hwmon;
25 static struct platform_device *rpi_clk;
26
27 struct rpi_firmware {
28         struct mbox_client cl;
29         struct mbox_chan *chan; /* The property channel. */
30         struct completion c;
31         u32 enabled;
32
33         struct kref consumers;
34         u32 get_throttled;
35 };
36
37 static struct platform_device *g_pdev;
38
39 static DEFINE_MUTEX(transaction_lock);
40
41 static void response_callback(struct mbox_client *cl, void *msg)
42 {
43         struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
44         complete(&fw->c);
45 }
46
47 /*
48  * Sends a request to the firmware through the BCM2835 mailbox driver,
49  * and synchronously waits for the reply.
50  */
51 static int
52 rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
53 {
54         u32 message = MBOX_MSG(chan, data);
55         int ret;
56
57         WARN_ON(data & 0xf);
58
59         mutex_lock(&transaction_lock);
60         reinit_completion(&fw->c);
61         ret = mbox_send_message(fw->chan, &message);
62         if (ret >= 0) {
63                 if (wait_for_completion_timeout(&fw->c, HZ)) {
64                         ret = 0;
65                 } else {
66                         ret = -ETIMEDOUT;
67                         WARN_ONCE(1, "Firmware transaction timeout");
68                 }
69         } else {
70                 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
71         }
72         mutex_unlock(&transaction_lock);
73
74         return ret;
75 }
76
77 /**
78  * rpi_firmware_property_list - Submit firmware property list
79  * @fw:         Pointer to firmware structure from rpi_firmware_get().
80  * @data:       Buffer holding tags.
81  * @tag_size:   Size of tags buffer.
82  *
83  * Submits a set of concatenated tags to the VPU firmware through the
84  * mailbox property interface.
85  *
86  * The buffer header and the ending tag are added by this function and
87  * don't need to be supplied, just the actual tags for your operation.
88  * See struct rpi_firmware_property_tag_header for the per-tag
89  * structure.
90  */
91 int rpi_firmware_property_list(struct rpi_firmware *fw,
92                                void *data, size_t tag_size)
93 {
94         size_t size = tag_size + 12;
95         u32 *buf;
96         dma_addr_t bus_addr;
97         int ret;
98
99         /* Packets are processed a dword at a time. */
100         if (size & 3)
101                 return -EINVAL;
102
103         buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
104                                  GFP_ATOMIC);
105         if (!buf)
106                 return -ENOMEM;
107
108         /* The firmware will error out without parsing in this case. */
109         WARN_ON(size >= 1024 * 1024);
110
111         buf[0] = size;
112         buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
113         memcpy(&buf[2], data, tag_size);
114         buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
115         wmb();
116
117         ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
118
119         rmb();
120         memcpy(data, &buf[2], tag_size);
121         if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
122                 /*
123                  * The tag name here might not be the one causing the
124                  * error, if there were multiple tags in the request.
125                  * But single-tag is the most common, so go with it.
126                  */
127                 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
128                         buf[2], buf[1]);
129                 ret = -EINVAL;
130         }
131
132         dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
133
134         return ret;
135 }
136 EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
137
138 /**
139  * rpi_firmware_property - Submit single firmware property
140  * @fw:         Pointer to firmware structure from rpi_firmware_get().
141  * @tag:        One of enum_mbox_property_tag.
142  * @tag_data:   Tag data buffer.
143  * @buf_size:   Buffer size.
144  *
145  * Submits a single tag to the VPU firmware through the mailbox
146  * property interface.
147  *
148  * This is a convenience wrapper around
149  * rpi_firmware_property_list() to avoid some of the
150  * boilerplate in property calls.
151  */
152 int rpi_firmware_property(struct rpi_firmware *fw,
153                           u32 tag, void *tag_data, size_t buf_size)
154 {
155         struct rpi_firmware_property_tag_header *header;
156         int ret;
157
158         /* Some mailboxes can use over 1k bytes. Rather than checking
159          * size and using stack or kmalloc depending on requirements,
160          * just use kmalloc. Mailboxes don't get called enough to worry
161          * too much about the time taken in the allocation.
162          */
163         void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
164
165         if (!data)
166                 return -ENOMEM;
167
168         header = data;
169         header->tag = tag;
170         header->buf_size = buf_size;
171         header->req_resp_size = 0;
172         memcpy(data + sizeof(*header), tag_data, buf_size);
173
174         ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
175
176         memcpy(tag_data, data + sizeof(*header), buf_size);
177
178         kfree(data);
179
180         if ((tag == RPI_FIRMWARE_GET_THROTTLED) &&
181              memcmp(&fw->get_throttled, tag_data, sizeof(fw->get_throttled))) {
182                 memcpy(&fw->get_throttled, tag_data, sizeof(fw->get_throttled));
183                 sysfs_notify(&fw->cl.dev->kobj, NULL, "get_throttled");
184         }
185
186         return ret;
187 }
188 EXPORT_SYMBOL_GPL(rpi_firmware_property);
189
190 static int rpi_firmware_notify_reboot(struct notifier_block *nb,
191                                       unsigned long action,
192                                       void *data)
193 {
194         struct rpi_firmware *fw;
195         struct platform_device *pdev = g_pdev;
196         u32 reboot_flags = 0;
197
198         if (!pdev)
199                 return 0;
200
201         fw = platform_get_drvdata(pdev);
202         if (!fw)
203                 return 0;
204
205         // The partition id is the first parameter followed by zero or
206         // more flags separated by spaces indicating the reason for the reboot.
207         //
208         // 'tryboot': Sets a one-shot flag which is cleared upon reboot and
209         //            causes the tryboot.txt to be loaded instead of config.txt
210         //            by the bootloader and the start.elf firmware.
211         //
212         //            This is intended to allow automatic fallback to a known
213         //            good image if an OS/FW upgrade fails.
214         //
215         // N.B. The firmware mechanism for storing reboot flags may vary
216         // on different Raspberry Pi models.
217         if (data && strstr(data, " tryboot"))
218                 reboot_flags |= 0x1;
219
220         // The mailbox might have been called earlier, directly via vcmailbox
221         // so only overwrite if reboot flags are passed to the reboot command.
222         if (reboot_flags)
223                 (void)rpi_firmware_property(fw, RPI_FIRMWARE_SET_REBOOT_FLAGS,
224                                 &reboot_flags, sizeof(reboot_flags));
225
226         (void)rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
227
228         return 0;
229 }
230
231 static ssize_t get_throttled_show(struct device *dev,
232                                   struct device_attribute *attr, char *buf)
233 {
234         struct rpi_firmware *fw = dev_get_drvdata(dev);
235
236         WARN_ONCE(1, "deprecated, use hwmon sysfs instead\n");
237
238         return sprintf(buf, "%x\n", fw->get_throttled);
239 }
240
241 static DEVICE_ATTR_RO(get_throttled);
242
243 static struct attribute *rpi_firmware_dev_attrs[] = {
244         &dev_attr_get_throttled.attr,
245         NULL,
246 };
247
248 static const struct attribute_group rpi_firmware_dev_group = {
249         .attrs = rpi_firmware_dev_attrs,
250 };
251
252 static void
253 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
254 {
255         time64_t date_and_time;
256         u32 packet;
257         static const char * const variant_strs[] = {
258                 "unknown",
259                 "start",
260                 "start_x",
261                 "start_db",
262                 "start_cd",
263         };
264         const char *variant_str = "cmd unsupported";
265         u32 variant;
266         int ret = rpi_firmware_property(fw,
267                                         RPI_FIRMWARE_GET_FIRMWARE_REVISION,
268                                         &packet, sizeof(packet));
269
270         if (ret)
271                 return;
272
273         /* This is not compatible with y2038 */
274         date_and_time = packet;
275
276         ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_FIRMWARE_VARIANT,
277                                     &variant, sizeof(variant));
278
279         if (!ret) {
280                 if (variant >= ARRAY_SIZE(variant_strs))
281                         variant = 0;
282                 variant_str = variant_strs[variant];
283         }
284
285         dev_info(fw->cl.dev,
286                  "Attached to firmware from %ptT, variant %s\n",
287                  &date_and_time, variant_str);
288 }
289
290 static void
291 rpi_firmware_print_firmware_hash(struct rpi_firmware *fw)
292 {
293         u32 hash[5];
294         int ret = rpi_firmware_property(fw,
295                                         RPI_FIRMWARE_GET_FIRMWARE_HASH,
296                                         hash, sizeof(hash));
297
298         if (ret)
299                 return;
300
301         dev_info(fw->cl.dev,
302                  "Firmware hash is %08x%08x%08x%08x%08x\n",
303                  hash[0], hash[1], hash[2], hash[3], hash[4]);
304 }
305
306 static void
307 rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
308 {
309         u32 packet;
310         int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
311                                         &packet, sizeof(packet));
312
313         if (ret)
314                 return;
315
316         rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
317                                                   -1, NULL, 0);
318
319         if (!IS_ERR_OR_NULL(rpi_hwmon)) {
320                 if (devm_device_add_group(dev, &rpi_firmware_dev_group))
321                         dev_err(dev, "Failed to create get_trottled attr\n");
322         }
323 }
324
325 static void rpi_register_clk_driver(struct device *dev)
326 {
327         struct device_node *firmware;
328
329         /*
330          * Earlier DTs don't have a node for the firmware clocks but
331          * rely on us creating a platform device by hand. If we do
332          * have a node for the firmware clocks, just bail out here.
333          */
334         firmware = of_get_compatible_child(dev->of_node,
335                                            "raspberrypi,firmware-clocks");
336         if (firmware) {
337                 of_node_put(firmware);
338                 return;
339         }
340
341         rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
342                                                 -1, NULL, 0);
343 }
344
345 static void rpi_firmware_delete(struct kref *kref)
346 {
347         struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
348                                                consumers);
349
350         mbox_free_channel(fw->chan);
351         kfree(fw);
352 }
353
354 void rpi_firmware_put(struct rpi_firmware *fw)
355 {
356         kref_put(&fw->consumers, rpi_firmware_delete);
357 }
358 EXPORT_SYMBOL_GPL(rpi_firmware_put);
359
360 static void devm_rpi_firmware_put(void *data)
361 {
362         struct rpi_firmware *fw = data;
363
364         rpi_firmware_put(fw);
365 }
366
367 static int rpi_firmware_probe(struct platform_device *pdev)
368 {
369         struct device *dev = &pdev->dev;
370         struct rpi_firmware *fw;
371
372         /*
373          * Memory will be freed by rpi_firmware_delete() once all users have
374          * released their firmware handles. Don't use devm_kzalloc() here.
375          */
376         fw = kzalloc(sizeof(*fw), GFP_KERNEL);
377         if (!fw)
378                 return -ENOMEM;
379
380         fw->cl.dev = dev;
381         fw->cl.rx_callback = response_callback;
382         fw->cl.tx_block = true;
383
384         fw->chan = mbox_request_channel(&fw->cl, 0);
385         if (IS_ERR(fw->chan)) {
386                 int ret = PTR_ERR(fw->chan);
387                 if (ret != -EPROBE_DEFER)
388                         dev_err(dev, "Failed to get mbox channel: %d\n", ret);
389                 return ret;
390         }
391
392         init_completion(&fw->c);
393         kref_init(&fw->consumers);
394
395         platform_set_drvdata(pdev, fw);
396         g_pdev = pdev;
397
398         rpi_firmware_print_firmware_revision(fw);
399         rpi_firmware_print_firmware_hash(fw);
400         rpi_register_hwmon_driver(dev, fw);
401         rpi_register_clk_driver(dev);
402
403         return 0;
404 }
405
406 static void rpi_firmware_shutdown(struct platform_device *pdev)
407 {
408         struct rpi_firmware *fw = platform_get_drvdata(pdev);
409
410         if (!fw)
411                 return;
412
413         rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
414 }
415
416 static int rpi_firmware_remove(struct platform_device *pdev)
417 {
418         struct rpi_firmware *fw = platform_get_drvdata(pdev);
419
420         platform_device_unregister(rpi_hwmon);
421         rpi_hwmon = NULL;
422         platform_device_unregister(rpi_clk);
423         rpi_clk = NULL;
424
425         rpi_firmware_put(fw);
426         g_pdev = NULL;
427
428         return 0;
429 }
430
431 /**
432  * rpi_firmware_get - Get pointer to rpi_firmware structure.
433  * @firmware_node:    Pointer to the firmware Device Tree node.
434  *
435  * The reference to rpi_firmware has to be released with rpi_firmware_put().
436  *
437  * Returns NULL is the firmware device is not ready.
438  */
439 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
440 {
441         struct platform_device *pdev = of_find_device_by_node(firmware_node);
442         struct rpi_firmware *fw;
443
444         if (!pdev)
445                 return NULL;
446
447         fw = platform_get_drvdata(pdev);
448         if (!fw)
449                 goto err_put_device;
450
451         if (!kref_get_unless_zero(&fw->consumers))
452                 goto err_put_device;
453
454         put_device(&pdev->dev);
455
456         return fw;
457
458 err_put_device:
459         put_device(&pdev->dev);
460         return NULL;
461 }
462 EXPORT_SYMBOL_GPL(rpi_firmware_get);
463
464 /**
465  * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
466  * @firmware_node:    Pointer to the firmware Device Tree node.
467  *
468  * Returns NULL is the firmware device is not ready.
469  */
470 struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
471                                            struct device_node *firmware_node)
472 {
473         struct rpi_firmware *fw;
474
475         fw = rpi_firmware_get(firmware_node);
476         if (!fw)
477                 return NULL;
478
479         if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
480                 return NULL;
481
482         return fw;
483 }
484 EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
485
486 static const struct of_device_id rpi_firmware_of_match[] = {
487         { .compatible = "raspberrypi,bcm2835-firmware", },
488         {},
489 };
490 MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
491
492 static struct platform_driver rpi_firmware_driver = {
493         .driver = {
494                 .name = "raspberrypi-firmware",
495                 .of_match_table = rpi_firmware_of_match,
496         },
497         .probe          = rpi_firmware_probe,
498         .shutdown       = rpi_firmware_shutdown,
499         .remove         = rpi_firmware_remove,
500 };
501
502 static struct notifier_block rpi_firmware_reboot_notifier = {
503         .notifier_call = rpi_firmware_notify_reboot,
504 };
505
506 static int __init rpi_firmware_init(void)
507 {
508         int ret = register_reboot_notifier(&rpi_firmware_reboot_notifier);
509         if (ret)
510                 goto out1;
511         ret = platform_driver_register(&rpi_firmware_driver);
512         if (ret)
513                 goto out2;
514
515         return 0;
516
517 out2:
518         unregister_reboot_notifier(&rpi_firmware_reboot_notifier);
519 out1:
520         return ret;
521 }
522 core_initcall(rpi_firmware_init);
523
524 static void __init rpi_firmware_exit(void)
525 {
526         platform_driver_unregister(&rpi_firmware_driver);
527         unregister_reboot_notifier(&rpi_firmware_reboot_notifier);
528 }
529 module_exit(rpi_firmware_exit);
530
531 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
532 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
533 MODULE_LICENSE("GPL v2");