Skip to content

Commit af311ff

Browse files
bebarinoandersson
authored andcommitted
firmware: qcom_scm: Cleanup code in qcom_scm_assign_mem()
There are some questionable coding styles in this function. It looks quite odd to deref a pointer with array indexing that only uses the first element. Also, destroying an input/output variable halfway through the function and then overwriting it on success is not clear. It's better to use a local variable and the kernel macros to step through each bit set in a bitmask and clearly show where outputs are set. Cc: Ian Jackson <ian.jackson@citrix.com> Cc: Julien Grall <julien.grall@arm.com> Cc: Bjorn Andersson <bjorn.andersson@linaro.org> Cc: Avaneesh Kumar Dwivedi <akdwived@codeaurora.org> Tested-by: Bjorn Andersson <bjorn.andersson@linaro.org> Signed-off-by: Stephen Boyd <swboyd@chromium.org> [bjorn: Changed for_each_set_bit() size to BITS_PER_LONG] Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
1 parent c8b08fc commit af311ff

2 files changed

Lines changed: 21 additions & 22 deletions

File tree

‎drivers/firmware/qcom_scm.c‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ EXPORT_SYMBOL(qcom_scm_set_remote_state);
434434
*/
435435
int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
436436
unsigned int *srcvm,
437-
struct qcom_scm_vmperm *newvm, int dest_cnt)
437+
const struct qcom_scm_vmperm *newvm,
438+
unsigned int dest_cnt)
438439
{
439440
struct qcom_scm_current_perm_info *destvm;
440441
struct qcom_scm_mem_map_info *mem_to_map;
@@ -449,11 +450,10 @@ int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
449450
int next_vm;
450451
__le32 *src;
451452
void *ptr;
452-
int ret;
453-
int len;
454-
int i;
453+
int ret, i, b;
454+
unsigned long srcvm_bits = *srcvm;
455455

456-
src_sz = hweight_long(*srcvm) * sizeof(*src);
456+
src_sz = hweight_long(srcvm_bits) * sizeof(*src);
457457
mem_to_map_sz = sizeof(*mem_to_map);
458458
dest_sz = dest_cnt * sizeof(*destvm);
459459
ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) +
@@ -466,28 +466,26 @@ int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
466466

467467
/* Fill source vmid detail */
468468
src = ptr;
469-
len = hweight_long(*srcvm);
470-
for (i = 0; i < len; i++) {
471-
src[i] = cpu_to_le32(ffs(*srcvm) - 1);
472-
*srcvm ^= 1 << (ffs(*srcvm) - 1);
473-
}
469+
i = 0;
470+
for_each_set_bit(b, &srcvm_bits, BITS_PER_LONG)
471+
src[i++] = cpu_to_le32(b);
474472

475473
/* Fill details of mem buff to map */
476474
mem_to_map = ptr + ALIGN(src_sz, SZ_64);
477475
mem_to_map_phys = ptr_phys + ALIGN(src_sz, SZ_64);
478-
mem_to_map[0].mem_addr = cpu_to_le64(mem_addr);
479-
mem_to_map[0].mem_size = cpu_to_le64(mem_sz);
476+
mem_to_map->mem_addr = cpu_to_le64(mem_addr);
477+
mem_to_map->mem_size = cpu_to_le64(mem_sz);
480478

481479
next_vm = 0;
482480
/* Fill details of next vmid detail */
483481
destvm = ptr + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
484482
dest_phys = ptr_phys + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
485-
for (i = 0; i < dest_cnt; i++) {
486-
destvm[i].vmid = cpu_to_le32(newvm[i].vmid);
487-
destvm[i].perm = cpu_to_le32(newvm[i].perm);
488-
destvm[i].ctx = 0;
489-
destvm[i].ctx_size = 0;
490-
next_vm |= BIT(newvm[i].vmid);
483+
for (i = 0; i < dest_cnt; i++, destvm++, newvm++) {
484+
destvm->vmid = cpu_to_le32(newvm->vmid);
485+
destvm->perm = cpu_to_le32(newvm->perm);
486+
destvm->ctx = 0;
487+
destvm->ctx_size = 0;
488+
next_vm |= BIT(newvm->vmid);
491489
}
492490

493491
ret = __qcom_scm_assign_mem(__scm->dev, mem_to_map_phys, mem_to_map_sz,

‎include/linux/qcom_scm.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ extern int qcom_scm_pas_mem_setup(u32 peripheral, phys_addr_t addr,
4949
extern int qcom_scm_pas_auth_and_reset(u32 peripheral);
5050
extern int qcom_scm_pas_shutdown(u32 peripheral);
5151
extern int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
52-
unsigned int *src, struct qcom_scm_vmperm *newvm,
53-
int dest_cnt);
52+
unsigned int *src,
53+
const struct qcom_scm_vmperm *newvm,
54+
unsigned int dest_cnt);
5455
extern void qcom_scm_cpu_power_down(u32 flags);
5556
extern u32 qcom_scm_get_version(void);
5657
extern int qcom_scm_set_remote_state(u32 state, u32 id);
@@ -87,8 +88,8 @@ qcom_scm_pas_auth_and_reset(u32 peripheral) { return -ENODEV; }
8788
static inline int qcom_scm_pas_shutdown(u32 peripheral) { return -ENODEV; }
8889
static inline int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
8990
unsigned int *src,
90-
struct qcom_scm_vmperm *newvm,
91-
int dest_cnt) { return -ENODEV; }
91+
const struct qcom_scm_vmperm *newvm,
92+
unsigned int dest_cnt) { return -ENODEV; }
9293
static inline void qcom_scm_cpu_power_down(u32 flags) {}
9394
static inline u32 qcom_scm_get_version(void) { return 0; }
9495
static inline u32

0 commit comments

Comments
 (0)