aboutsummaryrefslogtreecommitdiff
path: root/target/linux/qualcommax/ipq807x/base-files/etc/init.d/smp_affinity
blob: ecf88aea09581469268cb4632777f855fcfdb96f (plain)
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
#!/bin/sh /etc/rc.common
######################################################################
# vim: set ft=bash
# shellcheck disable=2155,3019,3043,3057,3060
######################################################################

START=93

PROG=smp_affinity

log_msg() {

  local irq_name="$1" affinity="$2" irq="$3"

  msg="$(printf "Pinning IRQ($irq) %-24s to CPU ${affinity}\n" "$irq_name")"

  logger -t "$PROG" "$msg"
}

######################################################################
### Takes a comma, space separated, or range list of CPU numbers and
## returns a bitmask of CPUs.
## cpus_to_bitmask "0,1,2,3" -> f
## cpus_to_bitmask "0 1 2 3" -> f
## cpus_to_bitmask "0-3"     -> f
## cpus_to_bitmask "3"       -> 8
#######################################################################

cpus_to_bitmask() {

  local bitmask=0
  # shellcheck disable=2048
  for range in ${*//,/ }; do
    start="${range%-*}"
    end="${range#*-}"
    if [ -z "$end" ]; then
      bitmask="$((bitmask | 1 << start))"
    else
      bitmask="$((bitmask | (2 ** (end - start + 1) - 1) << start))"
    fi
  done
  printf '%x' $bitmask
}

######################################################################
### Takes a bitmask of CPUs and returns a space separated list of
## CPU numbers.
## bitmask_to_cpus f -> 0 1 2 3
######################################################################

bitmask_to_cpus() {

  [ "${1:0:2}" != "0x" ] && set -- "0x$1"
  local bitmask="$(printf '%d' "$1")"

  local cpus=""
  for i in $(seq 0 63); do
    if [ $((bitmask & 1)) -ne 0 ]; then
      cpus="$cpus $i"
    fi
    bitmask=$((bitmask >> 1))
  done
  echo "${cpus# }"
}

######################################################################
### Sets the affinity of the IRQs with the given name to the given CPU.
## 1st argument: IRQ name ("reo2host-destination-ring1") (req)
## 2nd argument: CPU number (req)
######################################################################

set_affinity() {

  local irq_name="$1" affinity="$2" bitmask irq
  awk -v irq_name="$1" '$0 ~ irq_name { print substr($1, 1, length($1)-1); exit }' /proc/interrupts \
    | while read -r irq; do
      $enable_log && {
        log_msg "$irq_name" "$affinity" "$irq"
      }
      bitmask=$(cpus_to_bitmask "$affinity") && echo "$bitmask" > "/proc/irq/$irq/smp_affinity"
    done
}

enable_affinity_ipq807x() {

  # assign 4 rx interrupts to each core
  set_affinity 'reo2host-destination-ring1'    0
  set_affinity 'reo2host-destination-ring2'    1
  set_affinity 'reo2host-destination-ring3'    2
  set_affinity 'reo2host-destination-ring4'    3

  # assign 3 tcl completions to last 3 CPUs
  set_affinity 'wbm2host-tx-completions-ring1' 1
  set_affinity 'wbm2host-tx-completions-ring2' 2
  set_affinity 'wbm2host-tx-completions-ring3' 3

  # assign 3 ppdu mac interrupts to last 3 cores
  set_affinity 'ppdu-end-interrupts-mac1'      1
  set_affinity 'ppdu-end-interrupts-mac2'      2
  set_affinity 'ppdu-end-interrupts-mac3'      3

  # assign 4 lan/wan to core 4
  set_affinity 'edma_txcmpl'                   3
  set_affinity 'edma_rxfill'                   3
  set_affinity 'edma_rxdesc'                   3
  set_affinity 'edma_misc'                     3
}

boot() {

  local enable

  config_load     smp_affinity

  config_get_bool enable     "general" enable     1
  config_get_bool enable_log "general" enable_log 1

  [ "$enable"     -eq 1 ] && enable=true     || enable=false
  [ "$enable_log" -eq 1 ] && enable_log=true || enable_log=false

  $enable && enable_affinity_ipq807x
}