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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
// SPDX-License-Identifier: GPL-2.0
/*
* Support for Medifield PNW Camera Imaging ISP subsystem.
*
* Copyright (c) 2010-2017 Intel Corporation. All Rights Reserved.
*
* Copyright (c) 2010 Silicon Hive www.siliconhive.com.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version
* 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*/
/*
* This file contains entry functions for memory management of ISP driver
*/
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/highmem.h> /* for kmap */
#include <linux/io.h> /* for page_to_phys */
#include <linux/sysfs.h>
#include "hmm/hmm.h"
#include "hmm/hmm_bo.h"
#include "atomisp_internal.h"
#include "asm/cacheflush.h"
#include "mmu/isp_mmu.h"
#include "mmu/sh_mmu_mrfld.h"
struct hmm_bo_device bo_device;
static ia_css_ptr dummy_ptr = mmgr_EXCEPTION;
static bool hmm_initialized;
/*
* p: private
* v: vmalloc
*/
static const char hmm_bo_type_string[] = "pv";
static ssize_t bo_show(struct device *dev, struct device_attribute *attr,
char *buf, struct list_head *bo_list, bool active)
{
ssize_t ret = 0;
struct hmm_buffer_object *bo;
unsigned long flags;
int i;
long total[HMM_BO_LAST] = { 0 };
long count[HMM_BO_LAST] = { 0 };
int index1 = 0;
int index2 = 0;
ret = scnprintf(buf, PAGE_SIZE, "type pgnr\n");
if (ret <= 0)
return 0;
index1 += ret;
spin_lock_irqsave(&bo_device.list_lock, flags);
list_for_each_entry(bo, bo_list, list) {
if ((active && (bo->status & HMM_BO_ALLOCED)) ||
(!active && !(bo->status & HMM_BO_ALLOCED))) {
ret = scnprintf(buf + index1, PAGE_SIZE - index1,
"%c %d\n",
hmm_bo_type_string[bo->type], bo->pgnr);
total[bo->type] += bo->pgnr;
count[bo->type]++;
if (ret > 0)
index1 += ret;
}
}
spin_unlock_irqrestore(&bo_device.list_lock, flags);
for (i = 0; i < HMM_BO_LAST; i++) {
if (count[i]) {
ret = scnprintf(buf + index1 + index2,
PAGE_SIZE - index1 - index2,
"%ld %c buffer objects: %ld KB\n",
count[i], hmm_bo_type_string[i],
total[i] * 4);
if (ret > 0)
index2 += ret;
}
}
/* Add trailing zero, not included by scnprintf */
return index1 + index2 + 1;
}
static ssize_t active_bo_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
return bo_show(dev, attr, buf, &bo_device.entire_bo_list, true);
}
static ssize_t free_bo_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
return bo_show(dev, attr, buf, &bo_device.entire_bo_list, false);
}
static DEVICE_ATTR_RO(active_bo);
static DEVICE_ATTR_RO(free_bo);
static struct attribute *sysfs_attrs_ctrl[] = {
&dev_attr_active_bo.attr,
&dev_attr_free_bo.attr,
NULL
};
static struct attribute_group atomisp_attribute_group[] = {
{.attrs = sysfs_attrs_ctrl },
};
int hmm_init(void)
{
int ret;
ret = hmm_bo_device_init(&bo_device, &sh_mmu_mrfld,
ISP_VM_START, ISP_VM_SIZE);
if (ret)
dev_err(atomisp_dev, "hmm_bo_device_init failed.\n");
hmm_initialized = true;
/*
* As hmm use NULL to indicate invalid ISP virtual address,
* and ISP_VM_START is defined to 0 too, so we allocate
* one piece of dummy memory, which should return value 0,
* at the beginning, to avoid hmm_alloc return 0 in the
* further allocation.
*/
dummy_ptr = hmm_alloc(1);
if (!ret) {
ret = sysfs_create_group(&atomisp_dev->kobj,
atomisp_attribute_group);
if (ret)
dev_err(atomisp_dev,
"%s Failed to create sysfs\n", __func__);
}
return ret;
}
void hmm_cleanup(void)
{
if (dummy_ptr == mmgr_EXCEPTION)
return;
sysfs_remove_group(&atomisp_dev->kobj, atomisp_attribute_group);
/* free dummy memory first */
hmm_free(dummy_ptr);
dummy_ptr = 0;
hmm_bo_device_exit(&bo_device);
hmm_initialized = false;
}
static ia_css_ptr __hmm_alloc(size_t bytes, enum hmm_bo_type type,
void *vmalloc_addr)
{
unsigned int pgnr;
struct hmm_buffer_object *bo;
int ret;
/*
* Check if we are initialized. In the ideal world we wouldn't need
* this but we can tackle it once the driver is a lot cleaner
*/
if (!hmm_initialized)
hmm_init();
/* Get page number from size */
pgnr = size_to_pgnr_ceil(bytes);
/* Buffer object structure init */
bo = hmm_bo_alloc(&bo_device, pgnr);
if (!bo) {
dev_err(atomisp_dev, "hmm_bo_create failed.\n");
goto create_bo_err;
}
/* Allocate pages for memory */
ret = hmm_bo_alloc_pages(bo, type, vmalloc_addr);
if (ret) {
dev_err(atomisp_dev, "hmm_bo_alloc_pages failed.\n");
goto alloc_page_err;
}
/* Combine the virtual address and pages together */
ret = hmm_bo_bind(bo);
if (ret) {
dev_err(atomisp_dev, "hmm_bo_bind failed.\n");
goto bind_err;
}
dev_dbg(atomisp_dev, "pages: 0x%08x (%zu bytes), type: %d, vmalloc %p\n",
bo->start, bytes, type, vmalloc);
return bo->start;
bind_err:
hmm_bo_free_pages(bo);
alloc_page_err:
hmm_bo_unref(bo);
create_bo_err:
return 0;
}
ia_css_ptr hmm_alloc(size_t bytes)
{
return __hmm_alloc(bytes, HMM_BO_PRIVATE, NULL);
}
ia_css_ptr hmm_create_from_vmalloc_buf(size_t bytes, void *vmalloc_addr)
{
return __hmm_alloc(bytes, HMM_BO_VMALLOC, vmalloc_addr);
}
void hmm_free(ia_css_ptr virt)
{
struct hmm_buffer_object *bo;
dev_dbg(atomisp_dev, "%s: free 0x%08x\n", __func__, virt);
if (WARN_ON(virt == mmgr_EXCEPTION))
return;
bo = hmm_bo_device_search_start(&bo_device, (unsigned int)virt);
if (!bo) {
dev_err(atomisp_dev,
"can not find buffer object start with address 0x%x\n",
(unsigned int)virt);
return;
}
hmm_bo_unbind(bo);
hmm_bo_free_pages(bo);
hmm_bo_unref(bo);
}
static inline int hmm_check_bo(struct hmm_buffer_object *bo, unsigned int ptr)
{
if (!bo) {
dev_err(atomisp_dev,
"can not find buffer object contains address 0x%x\n",
ptr);
return -EINVAL;
}
if (!hmm_bo_page_allocated(bo)) {
dev_err(atomisp_dev,
"buffer object has no page allocated.\n");
return -EINVAL;
}
if (!hmm_bo_allocated(bo)) {
dev_err(atomisp_dev,
"buffer object has no virtual address space allocated.\n");
return -EINVAL;
}
return 0;
}
/* Read function in ISP memory management */
static int load_and_flush_by_kmap(ia_css_ptr virt, void *data,
unsigned int bytes)
{
struct hmm_buffer_object *bo;
unsigned int idx, offset, len;
char *src, *des;
int ret;
bo = hmm_bo_device_search_in_range(&bo_device, virt);
ret = hmm_check_bo(bo, virt);
if (ret)
return ret;
des = (char *)data;
while (bytes) {
idx = (virt - bo->start) >> PAGE_SHIFT;
offset = (virt - bo->start) - (idx << PAGE_SHIFT);
src = (char *)kmap_local_page(bo->pages[idx]) + offset;
if ((bytes + offset) >= PAGE_SIZE) {
len = PAGE_SIZE - offset;
bytes -= len;
} else {
len = bytes;
bytes = 0;
}
virt += len; /* update virt for next loop */
if (des) {
memcpy(des, src, len);
des += len;
}
clflush_cache_range(src, len);
kunmap_local(src);
}
return 0;
}
/* Read function in ISP memory management */
static int load_and_flush(ia_css_ptr virt, void *data, unsigned int bytes)
{
struct hmm_buffer_object *bo;
int ret;
bo = hmm_bo_device_search_in_range(&bo_device, virt);
ret = hmm_check_bo(bo, virt);
if (ret)
return ret;
if (bo->status & HMM_BO_VMAPED || bo->status & HMM_BO_VMAPED_CACHED) {
void *src = bo->vmap_addr;
src += (virt - bo->start);
memcpy(data, src, bytes);
if (bo->status & HMM_BO_VMAPED_CACHED)
clflush_cache_range(src, bytes);
} else {
void *vptr;
vptr = hmm_bo_vmap(bo, true);
if (!vptr)
return load_and_flush_by_kmap(virt, data, bytes);
else
vptr = vptr + (virt - bo->start);
memcpy(data, vptr, bytes);
clflush_cache_range(vptr, bytes);
hmm_bo_vunmap(bo);
}
return 0;
}
/* Read function in ISP memory management */
int hmm_load(ia_css_ptr virt, void *data, unsigned int bytes)
{
if (!virt) {
dev_warn(atomisp_dev,
"hmm_store: address is NULL\n");
return -EINVAL;
}
if (!data) {
dev_err(atomisp_dev,
"hmm_store: data is a NULL argument\n");
return -EINVAL;
}
return load_and_flush(virt, data, bytes);
}
/* Flush hmm data from the data cache */
int hmm_flush(ia_css_ptr virt, unsigned int bytes)
{
return load_and_flush(virt, NULL, bytes);
}
/* Write function in ISP memory management */
int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
{
struct hmm_buffer_object *bo;
unsigned int idx, offset, len;
char *src, *des;
int ret;
if (!virt) {
dev_warn(atomisp_dev,
"hmm_store: address is NULL\n");
return -EINVAL;
}
if (!data) {
dev_err(atomisp_dev,
"hmm_store: data is a NULL argument\n");
return -EINVAL;
}
bo = hmm_bo_device_search_in_range(&bo_device, virt);
ret = hmm_check_bo(bo, virt);
if (ret)
return ret;
if (bo->status & HMM_BO_VMAPED || bo->status & HMM_BO_VMAPED_CACHED) {
void *dst = bo->vmap_addr;
dst += (virt - bo->start);
memcpy(dst, data, bytes);
if (bo->status & HMM_BO_VMAPED_CACHED)
clflush_cache_range(dst, bytes);
} else {
void *vptr;
vptr = hmm_bo_vmap(bo, true);
if (vptr) {
vptr = vptr + (virt - bo->start);
memcpy(vptr, data, bytes);
clflush_cache_range(vptr, bytes);
hmm_bo_vunmap(bo);
return 0;
}
}
src = (char *)data;
while (bytes) {
idx = (virt - bo->start) >> PAGE_SHIFT;
offset = (virt - bo->start) - (idx << PAGE_SHIFT);
des = (char *)kmap_local_page(bo->pages[idx]);
if (!des) {
dev_err(atomisp_dev,
"kmap buffer object page failed: pg_idx = %d\n",
idx);
return -EINVAL;
}
des += offset;
if ((bytes + offset) >= PAGE_SIZE) {
len = PAGE_SIZE - offset;
bytes -= len;
} else {
len = bytes;
bytes = 0;
}
virt += len;
memcpy(des, src, len);
src += len;
clflush_cache_range(des, len);
kunmap_local(des);
}
return 0;
}
/* memset function in ISP memory management */
int hmm_set(ia_css_ptr virt, int c, unsigned int bytes)
{
struct hmm_buffer_object *bo;
unsigned int idx, offset, len;
char *des;
int ret;
bo = hmm_bo_device_search_in_range(&bo_device, virt);
ret = hmm_check_bo(bo, virt);
if (ret)
return ret;
if (bo->status & HMM_BO_VMAPED || bo->status & HMM_BO_VMAPED_CACHED) {
void *dst = bo->vmap_addr;
dst += (virt - bo->start);
memset(dst, c, bytes);
if (bo->status & HMM_BO_VMAPED_CACHED)
clflush_cache_range(dst, bytes);
} else {
void *vptr;
vptr = hmm_bo_vmap(bo, true);
if (vptr) {
vptr = vptr + (virt - bo->start);
memset(vptr, c, bytes);
clflush_cache_range(vptr, bytes);
hmm_bo_vunmap(bo);
return 0;
}
}
while (bytes) {
idx = (virt - bo->start) >> PAGE_SHIFT;
offset = (virt - bo->start) - (idx << PAGE_SHIFT);
des = (char *)kmap_local_page(bo->pages[idx]) + offset;
if ((bytes + offset) >= PAGE_SIZE) {
len = PAGE_SIZE - offset;
bytes -= len;
} else {
len = bytes;
bytes = 0;
}
virt += len;
memset(des, c, len);
clflush_cache_range(des, len);
kunmap_local(des);
}
return 0;
}
/* Virtual address to physical address convert */
phys_addr_t hmm_virt_to_phys(ia_css_ptr virt)
{
unsigned int idx, offset;
struct hmm_buffer_object *bo;
bo = hmm_bo_device_search_in_range(&bo_device, virt);
if (!bo) {
dev_err(atomisp_dev,
"can not find buffer object contains address 0x%x\n",
virt);
return -1;
}
idx = (virt - bo->start) >> PAGE_SHIFT;
offset = (virt - bo->start) - (idx << PAGE_SHIFT);
return page_to_phys(bo->pages[idx]) + offset;
}
int hmm_mmap(struct vm_area_struct *vma, ia_css_ptr virt)
{
struct hmm_buffer_object *bo;
bo = hmm_bo_device_search_start(&bo_device, virt);
if (!bo) {
dev_err(atomisp_dev,
"can not find buffer object start with address 0x%x\n",
virt);
return -EINVAL;
}
return hmm_bo_mmap(vma, bo);
}
/* Map ISP virtual address into IA virtual address */
void *hmm_vmap(ia_css_ptr virt, bool cached)
{
struct hmm_buffer_object *bo;
void *ptr;
bo = hmm_bo_device_search_in_range(&bo_device, virt);
if (!bo) {
dev_err(atomisp_dev,
"can not find buffer object contains address 0x%x\n",
virt);
return NULL;
}
ptr = hmm_bo_vmap(bo, cached);
if (ptr)
return ptr + (virt - bo->start);
else
return NULL;
}
/* Flush the memory which is mapped as cached memory through hmm_vmap */
void hmm_flush_vmap(ia_css_ptr virt)
{
struct hmm_buffer_object *bo;
bo = hmm_bo_device_search_in_range(&bo_device, virt);
if (!bo) {
dev_warn(atomisp_dev,
"can not find buffer object contains address 0x%x\n",
virt);
return;
}
hmm_bo_flush_vmap(bo);
}
void hmm_vunmap(ia_css_ptr virt)
{
struct hmm_buffer_object *bo;
bo = hmm_bo_device_search_in_range(&bo_device, virt);
if (!bo) {
dev_warn(atomisp_dev,
"can not find buffer object contains address 0x%x\n",
virt);
return;
}
hmm_bo_vunmap(bo);
}
|