hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2020 Rockchip Electronics Co., Ltd. */
 
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/of_graph.h>
#include <linux/of_platform.h>
#include <linux/pm_runtime.h>
#include <linux/pinctrl/consumer.h>
#include <linux/regmap.h>
#include <linux/dma-buf.h>
#include <linux/highmem.h>
#include <linux/soc/rockchip/rockchip_thunderboot_service.h>
 
#include "rkisp_tb_helper.h"
 
static struct platform_device *rkisp_tb_pdev;
static struct clk_bulk_data *rkisp_tb_clk;
static int rkisp_tb_clk_num;
static struct rk_tb_client tb_cl;
 
struct shm_data {
   int npages;
   struct page *pages[];
};
 
static struct sg_table *shm_map_dma_buf(struct dma_buf_attachment *attachment,
                   enum dma_data_direction dir)
{
   struct shm_data *data = attachment->dmabuf->priv;
   struct sg_table *table;
   struct scatterlist *sg;
   int i;
 
   table = kmalloc(sizeof(*table), GFP_KERNEL);
   if (!table)
       return ERR_PTR(-ENOMEM);
 
   sg_alloc_table(table, data->npages, GFP_KERNEL);
   sg = table->sgl;
   for (i = 0; i < data->npages; i++) {
       sg_set_page(sg, data->pages[i], PAGE_SIZE, 0);
       sg = sg_next(sg);
   }
 
   dma_map_sg_attrs(attachment->dev, table->sgl, table->nents, dir, DMA_ATTR_SKIP_CPU_SYNC);
 
   return table;
}
 
static void shm_unmap_dma_buf(struct dma_buf_attachment *attachment,
                 struct sg_table *table,
                 enum dma_data_direction dir)
{
   dma_unmap_sg(attachment->dev, table->sgl, table->nents, dir);
   sg_free_table(table);
   kfree(table);
}
 
static void shm_release(struct dma_buf *dma_buf)
{
   struct shm_data *data = dma_buf->priv;
 
   kfree(data);
}
 
static void *shm_vmap(struct dma_buf *dma_buf)
{
   struct shm_data *data = dma_buf->priv;
 
   return vm_map_ram(data->pages, data->npages, 0);
}
 
static void shm_vunmap(struct dma_buf *dma_buf, void *vaddr)
{
   struct shm_data *data = dma_buf->priv;
 
   vm_unmap_ram(vaddr, data->npages);
}
 
static int shm_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
{
   struct shm_data *data = dma_buf->priv;
   unsigned long vm_start = vma->vm_start;
   int i;
 
   for (i = 0; i < data->npages; i++) {
       remap_pfn_range(vma, vm_start, page_to_pfn(data->pages[i]),
               PAGE_SIZE, vma->vm_page_prot);
       vm_start += PAGE_SIZE;
   }
 
   return 0;
}
 
static int shm_begin_cpu_access(struct dma_buf *dmabuf, enum dma_data_direction dir)
{
   struct dma_buf_attachment *attachment;
   struct sg_table *table;
 
   attachment = list_first_entry(&dmabuf->attachments, struct dma_buf_attachment, node);
   table = attachment->priv;
   dma_sync_sg_for_cpu(NULL, table->sgl, table->nents, dir);
 
   return 0;
}
 
static int shm_end_cpu_access(struct dma_buf *dmabuf, enum dma_data_direction dir)
{
   struct dma_buf_attachment *attachment;
   struct sg_table *table;
 
   attachment = list_first_entry(&dmabuf->attachments, struct dma_buf_attachment, node);
   table = attachment->priv;
   dma_sync_sg_for_device(NULL, table->sgl, table->nents, dir);
 
   return 0;
}
 
static const struct dma_buf_ops shm_dmabuf_ops = {
   .map_dma_buf = shm_map_dma_buf,
   .unmap_dma_buf = shm_unmap_dma_buf,
   .release = shm_release,
   .mmap = shm_mmap,
   .vmap = shm_vmap,
   .vunmap = shm_vunmap,
   .begin_cpu_access = shm_begin_cpu_access,
   .end_cpu_access = shm_end_cpu_access,
};
 
static struct dma_buf *shm_alloc(struct rkisp_thunderboot_shmem *shmem)
{
   DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
   struct dma_buf *dmabuf;
   struct shm_data *data;
   int i, npages;
 
   npages = PAGE_ALIGN(shmem->shm_size) / PAGE_SIZE;
   data = kmalloc(sizeof(*data) + npages * sizeof(struct page *), GFP_KERNEL);
   if (!data)
       return ERR_PTR(-ENOMEM);
 
   data->npages = npages;
   for (i = 0; i < npages; i++)
       data->pages[i] = phys_to_page(shmem->shm_start + i * PAGE_SIZE);
 
   exp_info.ops = &shm_dmabuf_ops;
   exp_info.size = npages * PAGE_SIZE;
   exp_info.flags = O_RDWR;
   exp_info.priv = data;
 
   dmabuf = dma_buf_export(&exp_info);
 
   return dmabuf;
}
 
static int __maybe_unused rkisp_tb_clocks_loader_protect(void)
{
   int ret = 0;
 
   if (rkisp_tb_pdev) {
       pm_runtime_enable(&rkisp_tb_pdev->dev);
       pm_runtime_get_sync(&rkisp_tb_pdev->dev);
       if (rkisp_tb_clk_num) {
           ret = clk_bulk_prepare_enable(rkisp_tb_clk_num, rkisp_tb_clk);
           if (ret)
               dev_err(&rkisp_tb_pdev->dev, "Cannot enable clock\n");
       }
   }
 
   return ret;
}
 
static int __maybe_unused rkisp_tb_clocks_loader_unprotect(void)
{
   if (rkisp_tb_pdev) {
       if (rkisp_tb_clk_num)
           clk_bulk_disable_unprepare(rkisp_tb_clk_num, rkisp_tb_clk);
       pm_runtime_put_sync(&rkisp_tb_pdev->dev);
       pm_runtime_disable(&rkisp_tb_pdev->dev);
   }
   return 0;
}
 
static void rkisp_tb_cb(void *data)
{
   rkisp_tb_clocks_loader_unprotect();
}
 
static int __maybe_unused rkisp_tb_runtime_suspend(struct device *dev)
{
   return 0;
}
 
static int __maybe_unused rkisp_tb_runtime_resume(struct device *dev)
{
   return 0;
}
 
static const struct dev_pm_ops rkisp_tb_plat_pm_ops = {
   SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
               pm_runtime_force_resume)
   SET_RUNTIME_PM_OPS(rkisp_tb_runtime_suspend,
              rkisp_tb_runtime_resume, NULL)
};
 
static const struct of_device_id rkisp_tb_plat_of_match[] = {
   {
       .compatible = "rockchip,thunder-boot-rkisp",
   },
   {},
};
 
static int rkisp_tb_plat_probe(struct platform_device *pdev)
{
   rkisp_tb_pdev = pdev;
   rkisp_tb_clk_num = devm_clk_bulk_get_all(&pdev->dev, &rkisp_tb_clk);
   if (rkisp_tb_clk_num <= 0) {
       dev_warn(&pdev->dev, "get clk fail:%d\n", rkisp_tb_clk_num);
       rkisp_tb_clk_num = 0;
   }
   rkisp_tb_clocks_loader_protect();
 
   if (IS_ENABLED(CONFIG_ROCKCHIP_THUNDER_BOOT_SERVICE)) {
       tb_cl.cb = rkisp_tb_cb;
       return rk_tb_client_register_cb(&tb_cl);
   }
 
   return 0;
}
 
static int rkisp_tb_plat_remove(struct platform_device *pdev)
{
   return 0;
}
 
static struct platform_driver __maybe_unused rkisp_tb_plat_drv = {
   .driver = {
       .name = "rkisp_thunderboot",
       .of_match_table = of_match_ptr(rkisp_tb_plat_of_match),
       .pm = &rkisp_tb_plat_pm_ops,
   },
   .probe = rkisp_tb_plat_probe,
   .remove = rkisp_tb_plat_remove,
};
 
static int __init rkisp_tb_plat_drv_init(void)
{
   return platform_driver_register(&rkisp_tb_plat_drv);
}
 
arch_initcall_sync(rkisp_tb_plat_drv_init);
 
long rkisp_tb_shm_ioctl(struct rkisp_thunderboot_shmem *shmem)
{
   struct dma_buf *dmabuf;
   int fd, ret;
 
   dmabuf = shm_alloc(shmem);
   if (IS_ERR(dmabuf)) {
       ret = PTR_ERR(dmabuf);
       return ret;
   }
 
   fd = dma_buf_fd(dmabuf, O_CLOEXEC);
   shmem->shm_fd = fd;
 
   return 0;
}
 
void rkisp_tb_unprotect_clk(void)
{
   if (IS_ENABLED(CONFIG_ROCKCHIP_THUNDER_BOOT_SERVICE))
       return;
 
   rkisp_tb_clocks_loader_unprotect();
}
EXPORT_SYMBOL(rkisp_tb_unprotect_clk);
 
static enum rkisp_tb_state tb_state;
 
void rkisp_tb_set_state(enum rkisp_tb_state result)
{
   tb_state = result;
}
EXPORT_SYMBOL(rkisp_tb_set_state);
 
enum rkisp_tb_state rkisp_tb_get_state(void)
{
   return tb_state;
}
EXPORT_SYMBOL(rkisp_tb_get_state);