@@ -1735,26 +1735,37 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
1735
1735
return 0 ;
1736
1736
}
1737
1737
1738
+ static int arm_smmu_streams_cmp_key (const void * lhs , const struct rb_node * rhs )
1739
+ {
1740
+ struct arm_smmu_stream * stream_rhs =
1741
+ rb_entry (rhs , struct arm_smmu_stream , node );
1742
+ const u32 * sid_lhs = lhs ;
1743
+
1744
+ if (* sid_lhs < stream_rhs -> id )
1745
+ return -1 ;
1746
+ if (* sid_lhs > stream_rhs -> id )
1747
+ return 1 ;
1748
+ return 0 ;
1749
+ }
1750
+
1751
+ static int arm_smmu_streams_cmp_node (struct rb_node * lhs ,
1752
+ const struct rb_node * rhs )
1753
+ {
1754
+ return arm_smmu_streams_cmp_key (
1755
+ & rb_entry (lhs , struct arm_smmu_stream , node )-> id , rhs );
1756
+ }
1757
+
1738
1758
static struct arm_smmu_master *
1739
1759
arm_smmu_find_master (struct arm_smmu_device * smmu , u32 sid )
1740
1760
{
1741
1761
struct rb_node * node ;
1742
- struct arm_smmu_stream * stream ;
1743
1762
1744
1763
lockdep_assert_held (& smmu -> streams_mutex );
1745
1764
1746
- node = smmu -> streams .rb_node ;
1747
- while (node ) {
1748
- stream = rb_entry (node , struct arm_smmu_stream , node );
1749
- if (stream -> id < sid )
1750
- node = node -> rb_right ;
1751
- else if (stream -> id > sid )
1752
- node = node -> rb_left ;
1753
- else
1754
- return stream -> master ;
1755
- }
1756
-
1757
- return NULL ;
1765
+ node = rb_find (& sid , & smmu -> streams , arm_smmu_streams_cmp_key );
1766
+ if (!node )
1767
+ return NULL ;
1768
+ return rb_entry (node , struct arm_smmu_stream , node )-> master ;
1758
1769
}
1759
1770
1760
1771
/* IRQ and event handlers */
@@ -3210,8 +3221,6 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
3210
3221
{
3211
3222
int i ;
3212
3223
int ret = 0 ;
3213
- struct arm_smmu_stream * new_stream , * cur_stream ;
3214
- struct rb_node * * new_node , * parent_node = NULL ;
3215
3224
struct iommu_fwspec * fwspec = dev_iommu_fwspec_get (master -> dev );
3216
3225
3217
3226
master -> streams = kcalloc (fwspec -> num_ids , sizeof (* master -> streams ),
@@ -3222,9 +3231,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
3222
3231
3223
3232
mutex_lock (& smmu -> streams_mutex );
3224
3233
for (i = 0 ; i < fwspec -> num_ids ; i ++ ) {
3234
+ struct arm_smmu_stream * new_stream = & master -> streams [i ];
3225
3235
u32 sid = fwspec -> ids [i ];
3226
3236
3227
- new_stream = & master -> streams [i ];
3228
3237
new_stream -> id = sid ;
3229
3238
new_stream -> master = master ;
3230
3239
@@ -3233,28 +3242,13 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
3233
3242
break ;
3234
3243
3235
3244
/* Insert into SID tree */
3236
- new_node = & (smmu -> streams .rb_node );
3237
- while (* new_node ) {
3238
- cur_stream = rb_entry (* new_node , struct arm_smmu_stream ,
3239
- node );
3240
- parent_node = * new_node ;
3241
- if (cur_stream -> id > new_stream -> id ) {
3242
- new_node = & ((* new_node )-> rb_left );
3243
- } else if (cur_stream -> id < new_stream -> id ) {
3244
- new_node = & ((* new_node )-> rb_right );
3245
- } else {
3246
- dev_warn (master -> dev ,
3247
- "stream %u already in tree\n" ,
3248
- cur_stream -> id );
3249
- ret = - EINVAL ;
3250
- break ;
3251
- }
3252
- }
3253
- if (ret )
3245
+ if (rb_find_add (& new_stream -> node , & smmu -> streams ,
3246
+ arm_smmu_streams_cmp_node )) {
3247
+ dev_warn (master -> dev , "stream %u already in tree\n" ,
3248
+ sid );
3249
+ ret = - EINVAL ;
3254
3250
break ;
3255
-
3256
- rb_link_node (& new_stream -> node , parent_node , new_node );
3257
- rb_insert_color (& new_stream -> node , & smmu -> streams );
3251
+ }
3258
3252
}
3259
3253
3260
3254
if (ret ) {
0 commit comments