accf3e968606b84e1b35653ec82b9c1f4018ee68
[openwrt/staging/dedeckeh.git] / target / linux / ipq40xx / patches-5.15 / 421-firmware-qcom-scm-cold-boot-address.patch
1 --- a/drivers/firmware/qcom_scm-legacy.c
2 +++ b/drivers/firmware/qcom_scm-legacy.c
3 @@ -13,6 +13,9 @@
4 #include <linux/arm-smccc.h>
5 #include <linux/dma-mapping.h>
6
7 +#include <asm/cacheflush.h>
8 +#include <asm/outercache.h>
9 +
10 #include "qcom_scm.h"
11
12 static DEFINE_MUTEX(qcom_scm_lock);
13 @@ -117,6 +120,25 @@ static void __scm_legacy_do(const struct
14 } while (res->a0 == QCOM_SCM_INTERRUPTED);
15 }
16
17 +static void qcom_scm_inv_range(unsigned long start, unsigned long end)
18 +{
19 + u32 cacheline_size, ctr;
20 +
21 + asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
22 + cacheline_size = 4 << ((ctr >> 16) & 0xf);
23 +
24 + start = round_down(start, cacheline_size);
25 + end = round_up(end, cacheline_size);
26 + outer_inv_range(start, end);
27 + while (start < end) {
28 + asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
29 + : "memory");
30 + start += cacheline_size;
31 + }
32 + dsb();
33 + isb();
34 +}
35 +
36 /**
37 * qcom_scm_call() - Sends a command to the SCM and waits for the command to
38 * finish processing.
39 @@ -160,10 +182,16 @@ int scm_legacy_call(struct device *dev,
40
41 rsp = scm_legacy_command_to_response(cmd);
42
43 - cmd_phys = dma_map_single(dev, cmd, alloc_len, DMA_TO_DEVICE);
44 - if (dma_mapping_error(dev, cmd_phys)) {
45 - kfree(cmd);
46 - return -ENOMEM;
47 + if (dev) {
48 + cmd_phys = dma_map_single(dev, cmd, alloc_len, DMA_TO_DEVICE);
49 + if (dma_mapping_error(dev, cmd_phys)) {
50 + kfree(cmd);
51 + return -ENOMEM;
52 + }
53 + } else {
54 + cmd_phys = virt_to_phys(cmd);
55 + __cpuc_flush_dcache_area(cmd, alloc_len);
56 + outer_flush_range(cmd_phys, cmd_phys + alloc_len);
57 }
58
59 smc.args[0] = 1;
60 @@ -179,13 +207,26 @@ int scm_legacy_call(struct device *dev,
61 goto out;
62
63 do {
64 - dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len,
65 - sizeof(*rsp), DMA_FROM_DEVICE);
66 + if (dev) {
67 + dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) +
68 + cmd_len, sizeof(*rsp),
69 + DMA_FROM_DEVICE);
70 + } else {
71 + unsigned long start = (uintptr_t)cmd + sizeof(*cmd) +
72 + cmd_len;
73 + qcom_scm_inv_range(start, start + sizeof(*rsp));
74 + }
75 } while (!rsp->is_complete);
76
77 - dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len +
78 - le32_to_cpu(rsp->buf_offset),
79 - resp_len, DMA_FROM_DEVICE);
80 + if (dev) {
81 + dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len +
82 + le32_to_cpu(rsp->buf_offset),
83 + resp_len, DMA_FROM_DEVICE);
84 + } else {
85 + unsigned long start = (uintptr_t)cmd + sizeof(*cmd) + cmd_len +
86 + le32_to_cpu(rsp->buf_offset);
87 + qcom_scm_inv_range(start, start + resp_len);
88 + }
89
90 if (res) {
91 res_buf = scm_legacy_get_response_buffer(rsp);
92 @@ -193,7 +234,8 @@ int scm_legacy_call(struct device *dev,
93 res->result[i] = le32_to_cpu(res_buf[i]);
94 }
95 out:
96 - dma_unmap_single(dev, cmd_phys, alloc_len, DMA_TO_DEVICE);
97 + if (dev)
98 + dma_unmap_single(dev, cmd_phys, alloc_len, DMA_TO_DEVICE);
99 kfree(cmd);
100 return ret;
101 }
102 --- a/drivers/firmware/qcom_scm.c
103 +++ b/drivers/firmware/qcom_scm.c
104 @@ -344,6 +344,17 @@ int qcom_scm_set_cold_boot_addr(void *en
105 desc.args[0] = flags;
106 desc.args[1] = virt_to_phys(entry);
107
108 + /*
109 + * Factory firmware doesn't support the atomic variant. Non-atomic SCMs
110 + * require ugly DMA invalidation support that was dropped upstream a
111 + * while ago. For more info, see:
112 + *
113 + * [RFC] qcom_scm: IPQ4019 firmware does not support atomic API?
114 + * https://lore.kernel.org/linux-arm-msm/20200913201608.GA3162100@bDebian/
115 + */
116 + if (of_machine_is_compatible("google,wifi"))
117 + return qcom_scm_call(__scm ? __scm->dev : NULL, &desc, NULL);
118 +
119 return qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
120 }
121 EXPORT_SYMBOL(qcom_scm_set_cold_boot_addr);