icc-bwmon.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
  4. * Copyright (C) 2021-2022 Linaro Ltd
  5. * Author: Krzysztof Kozlowski <[email protected]>, based on
  6. * previous work of Thara Gopinath and msm-4.9 downstream sources.
  7. */
  8. #include <linux/err.h>
  9. #include <linux/interconnect.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_opp.h>
  17. #include <linux/regmap.h>
  18. #include <linux/sizes.h>
  19. /*
  20. * The BWMON samples data throughput within 'sample_ms' time. With three
  21. * configurable thresholds (Low, Medium and High) gives four windows (called
  22. * zones) of current bandwidth:
  23. *
  24. * Zone 0: byte count < THRES_LO
  25. * Zone 1: THRES_LO < byte count < THRES_MED
  26. * Zone 2: THRES_MED < byte count < THRES_HIGH
  27. * Zone 3: THRES_HIGH < byte count
  28. *
  29. * Zones 0 and 2 are not used by this driver.
  30. */
  31. /* Internal sampling clock frequency */
  32. #define HW_TIMER_HZ 19200000
  33. #define BWMON_V4_GLOBAL_IRQ_CLEAR 0x008
  34. #define BWMON_V4_GLOBAL_IRQ_ENABLE 0x00c
  35. /*
  36. * All values here and further are matching regmap fields, so without absolute
  37. * register offsets.
  38. */
  39. #define BWMON_V4_GLOBAL_IRQ_ENABLE_ENABLE BIT(0)
  40. #define BWMON_V4_IRQ_STATUS 0x100
  41. #define BWMON_V4_IRQ_CLEAR 0x108
  42. #define BWMON_V4_IRQ_ENABLE 0x10c
  43. #define BWMON_IRQ_ENABLE_MASK (BIT(1) | BIT(3))
  44. #define BWMON_V5_IRQ_STATUS 0x000
  45. #define BWMON_V5_IRQ_CLEAR 0x008
  46. #define BWMON_V5_IRQ_ENABLE 0x00c
  47. #define BWMON_V4_ENABLE 0x2a0
  48. #define BWMON_V5_ENABLE 0x010
  49. #define BWMON_ENABLE_ENABLE BIT(0)
  50. #define BWMON_V4_CLEAR 0x2a4
  51. #define BWMON_V5_CLEAR 0x014
  52. #define BWMON_CLEAR_CLEAR BIT(0)
  53. #define BWMON_CLEAR_CLEAR_ALL BIT(1)
  54. #define BWMON_V4_SAMPLE_WINDOW 0x2a8
  55. #define BWMON_V5_SAMPLE_WINDOW 0x020
  56. #define BWMON_V4_THRESHOLD_HIGH 0x2ac
  57. #define BWMON_V4_THRESHOLD_MED 0x2b0
  58. #define BWMON_V4_THRESHOLD_LOW 0x2b4
  59. #define BWMON_V5_THRESHOLD_HIGH 0x024
  60. #define BWMON_V5_THRESHOLD_MED 0x028
  61. #define BWMON_V5_THRESHOLD_LOW 0x02c
  62. #define BWMON_V4_ZONE_ACTIONS 0x2b8
  63. #define BWMON_V5_ZONE_ACTIONS 0x030
  64. /*
  65. * Actions to perform on some zone 'z' when current zone hits the threshold:
  66. * Increment counter of zone 'z'
  67. */
  68. #define BWMON_ZONE_ACTIONS_INCREMENT(z) (0x2 << ((z) * 2))
  69. /* Clear counter of zone 'z' */
  70. #define BWMON_ZONE_ACTIONS_CLEAR(z) (0x1 << ((z) * 2))
  71. /* Zone 0 threshold hit: Clear zone count */
  72. #define BWMON_ZONE_ACTIONS_ZONE0 (BWMON_ZONE_ACTIONS_CLEAR(0))
  73. /* Zone 1 threshold hit: Increment zone count & clear lower zones */
  74. #define BWMON_ZONE_ACTIONS_ZONE1 (BWMON_ZONE_ACTIONS_INCREMENT(1) | \
  75. BWMON_ZONE_ACTIONS_CLEAR(0))
  76. /* Zone 2 threshold hit: Increment zone count & clear lower zones */
  77. #define BWMON_ZONE_ACTIONS_ZONE2 (BWMON_ZONE_ACTIONS_INCREMENT(2) | \
  78. BWMON_ZONE_ACTIONS_CLEAR(1) | \
  79. BWMON_ZONE_ACTIONS_CLEAR(0))
  80. /* Zone 3 threshold hit: Increment zone count & clear lower zones */
  81. #define BWMON_ZONE_ACTIONS_ZONE3 (BWMON_ZONE_ACTIONS_INCREMENT(3) | \
  82. BWMON_ZONE_ACTIONS_CLEAR(2) | \
  83. BWMON_ZONE_ACTIONS_CLEAR(1) | \
  84. BWMON_ZONE_ACTIONS_CLEAR(0))
  85. /*
  86. * There is no clear documentation/explanation of BWMON_V4_THRESHOLD_COUNT
  87. * register. Based on observations, this is number of times one threshold has to
  88. * be reached, to trigger interrupt in given zone.
  89. *
  90. * 0xff are maximum values meant to ignore the zones 0 and 2.
  91. */
  92. #define BWMON_V4_THRESHOLD_COUNT 0x2bc
  93. #define BWMON_V5_THRESHOLD_COUNT 0x034
  94. #define BWMON_THRESHOLD_COUNT_ZONE0_DEFAULT 0xff
  95. #define BWMON_THRESHOLD_COUNT_ZONE2_DEFAULT 0xff
  96. #define BWMON_V4_ZONE_MAX(zone) (0x2e0 + 4 * (zone))
  97. #define BWMON_V5_ZONE_MAX(zone) (0x044 + 4 * (zone))
  98. /* Quirks for specific BWMON types */
  99. #define BWMON_HAS_GLOBAL_IRQ BIT(0)
  100. #define BWMON_NEEDS_FORCE_CLEAR BIT(1)
  101. enum bwmon_fields {
  102. F_GLOBAL_IRQ_CLEAR,
  103. F_GLOBAL_IRQ_ENABLE,
  104. F_IRQ_STATUS,
  105. F_IRQ_CLEAR,
  106. F_IRQ_ENABLE,
  107. F_ENABLE,
  108. F_CLEAR,
  109. F_SAMPLE_WINDOW,
  110. F_THRESHOLD_HIGH,
  111. F_THRESHOLD_MED,
  112. F_THRESHOLD_LOW,
  113. F_ZONE_ACTIONS_ZONE0,
  114. F_ZONE_ACTIONS_ZONE1,
  115. F_ZONE_ACTIONS_ZONE2,
  116. F_ZONE_ACTIONS_ZONE3,
  117. F_THRESHOLD_COUNT_ZONE0,
  118. F_THRESHOLD_COUNT_ZONE1,
  119. F_THRESHOLD_COUNT_ZONE2,
  120. F_THRESHOLD_COUNT_ZONE3,
  121. F_ZONE0_MAX,
  122. F_ZONE1_MAX,
  123. F_ZONE2_MAX,
  124. F_ZONE3_MAX,
  125. F_NUM_FIELDS
  126. };
  127. struct icc_bwmon_data {
  128. unsigned int sample_ms;
  129. unsigned int count_unit_kb; /* kbytes */
  130. unsigned int default_highbw_kbps;
  131. unsigned int default_medbw_kbps;
  132. unsigned int default_lowbw_kbps;
  133. u8 zone1_thres_count;
  134. u8 zone3_thres_count;
  135. unsigned int quirks;
  136. const struct regmap_config *regmap_cfg;
  137. const struct reg_field *regmap_fields;
  138. };
  139. struct icc_bwmon {
  140. struct device *dev;
  141. const struct icc_bwmon_data *data;
  142. int irq;
  143. struct regmap *regmap;
  144. struct regmap_field *regs[F_NUM_FIELDS];
  145. unsigned int max_bw_kbps;
  146. unsigned int min_bw_kbps;
  147. unsigned int target_kbps;
  148. unsigned int current_kbps;
  149. };
  150. /* BWMON v4 */
  151. static const struct reg_field msm8998_bwmon_reg_fields[] = {
  152. [F_GLOBAL_IRQ_CLEAR] = REG_FIELD(BWMON_V4_GLOBAL_IRQ_CLEAR, 0, 0),
  153. [F_GLOBAL_IRQ_ENABLE] = REG_FIELD(BWMON_V4_GLOBAL_IRQ_ENABLE, 0, 0),
  154. [F_IRQ_STATUS] = REG_FIELD(BWMON_V4_IRQ_STATUS, 4, 7),
  155. [F_IRQ_CLEAR] = REG_FIELD(BWMON_V4_IRQ_CLEAR, 4, 7),
  156. [F_IRQ_ENABLE] = REG_FIELD(BWMON_V4_IRQ_ENABLE, 4, 7),
  157. /* F_ENABLE covers entire register to disable other features */
  158. [F_ENABLE] = REG_FIELD(BWMON_V4_ENABLE, 0, 31),
  159. [F_CLEAR] = REG_FIELD(BWMON_V4_CLEAR, 0, 1),
  160. [F_SAMPLE_WINDOW] = REG_FIELD(BWMON_V4_SAMPLE_WINDOW, 0, 23),
  161. [F_THRESHOLD_HIGH] = REG_FIELD(BWMON_V4_THRESHOLD_HIGH, 0, 11),
  162. [F_THRESHOLD_MED] = REG_FIELD(BWMON_V4_THRESHOLD_MED, 0, 11),
  163. [F_THRESHOLD_LOW] = REG_FIELD(BWMON_V4_THRESHOLD_LOW, 0, 11),
  164. [F_ZONE_ACTIONS_ZONE0] = REG_FIELD(BWMON_V4_ZONE_ACTIONS, 0, 7),
  165. [F_ZONE_ACTIONS_ZONE1] = REG_FIELD(BWMON_V4_ZONE_ACTIONS, 8, 15),
  166. [F_ZONE_ACTIONS_ZONE2] = REG_FIELD(BWMON_V4_ZONE_ACTIONS, 16, 23),
  167. [F_ZONE_ACTIONS_ZONE3] = REG_FIELD(BWMON_V4_ZONE_ACTIONS, 24, 31),
  168. [F_THRESHOLD_COUNT_ZONE0] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT, 0, 7),
  169. [F_THRESHOLD_COUNT_ZONE1] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT, 8, 15),
  170. [F_THRESHOLD_COUNT_ZONE2] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT, 16, 23),
  171. [F_THRESHOLD_COUNT_ZONE3] = REG_FIELD(BWMON_V4_THRESHOLD_COUNT, 24, 31),
  172. [F_ZONE0_MAX] = REG_FIELD(BWMON_V4_ZONE_MAX(0), 0, 11),
  173. [F_ZONE1_MAX] = REG_FIELD(BWMON_V4_ZONE_MAX(1), 0, 11),
  174. [F_ZONE2_MAX] = REG_FIELD(BWMON_V4_ZONE_MAX(2), 0, 11),
  175. [F_ZONE3_MAX] = REG_FIELD(BWMON_V4_ZONE_MAX(3), 0, 11),
  176. };
  177. static const struct regmap_range msm8998_bwmon_reg_noread_ranges[] = {
  178. regmap_reg_range(BWMON_V4_GLOBAL_IRQ_CLEAR, BWMON_V4_GLOBAL_IRQ_CLEAR),
  179. regmap_reg_range(BWMON_V4_IRQ_CLEAR, BWMON_V4_IRQ_CLEAR),
  180. regmap_reg_range(BWMON_V4_CLEAR, BWMON_V4_CLEAR),
  181. };
  182. static const struct regmap_access_table msm8998_bwmon_reg_read_table = {
  183. .no_ranges = msm8998_bwmon_reg_noread_ranges,
  184. .n_no_ranges = ARRAY_SIZE(msm8998_bwmon_reg_noread_ranges),
  185. };
  186. static const struct regmap_range msm8998_bwmon_reg_volatile_ranges[] = {
  187. regmap_reg_range(BWMON_V4_IRQ_STATUS, BWMON_V4_IRQ_STATUS),
  188. regmap_reg_range(BWMON_V4_ZONE_MAX(0), BWMON_V4_ZONE_MAX(3)),
  189. };
  190. static const struct regmap_access_table msm8998_bwmon_reg_volatile_table = {
  191. .yes_ranges = msm8998_bwmon_reg_volatile_ranges,
  192. .n_yes_ranges = ARRAY_SIZE(msm8998_bwmon_reg_volatile_ranges),
  193. };
  194. /*
  195. * Fill the cache for non-readable registers only as rest does not really
  196. * matter and can be read from the device.
  197. */
  198. static const struct reg_default msm8998_bwmon_reg_defaults[] = {
  199. { BWMON_V4_GLOBAL_IRQ_CLEAR, 0x0 },
  200. { BWMON_V4_IRQ_CLEAR, 0x0 },
  201. { BWMON_V4_CLEAR, 0x0 },
  202. };
  203. static const struct regmap_config msm8998_bwmon_regmap_cfg = {
  204. .reg_bits = 32,
  205. .reg_stride = 4,
  206. .val_bits = 32,
  207. /*
  208. * No concurrent access expected - driver has one interrupt handler,
  209. * regmap is not shared, no driver or user-space API.
  210. */
  211. .disable_locking = true,
  212. .rd_table = &msm8998_bwmon_reg_read_table,
  213. .volatile_table = &msm8998_bwmon_reg_volatile_table,
  214. .reg_defaults = msm8998_bwmon_reg_defaults,
  215. .num_reg_defaults = ARRAY_SIZE(msm8998_bwmon_reg_defaults),
  216. /*
  217. * Cache is necessary for using regmap fields with non-readable
  218. * registers.
  219. */
  220. .cache_type = REGCACHE_RBTREE,
  221. };
  222. /* BWMON v5 */
  223. static const struct reg_field sdm845_llcc_bwmon_reg_fields[] = {
  224. [F_GLOBAL_IRQ_CLEAR] = {},
  225. [F_GLOBAL_IRQ_ENABLE] = {},
  226. [F_IRQ_STATUS] = REG_FIELD(BWMON_V5_IRQ_STATUS, 0, 3),
  227. [F_IRQ_CLEAR] = REG_FIELD(BWMON_V5_IRQ_CLEAR, 0, 3),
  228. [F_IRQ_ENABLE] = REG_FIELD(BWMON_V5_IRQ_ENABLE, 0, 3),
  229. /* F_ENABLE covers entire register to disable other features */
  230. [F_ENABLE] = REG_FIELD(BWMON_V5_ENABLE, 0, 31),
  231. [F_CLEAR] = REG_FIELD(BWMON_V5_CLEAR, 0, 1),
  232. [F_SAMPLE_WINDOW] = REG_FIELD(BWMON_V5_SAMPLE_WINDOW, 0, 19),
  233. [F_THRESHOLD_HIGH] = REG_FIELD(BWMON_V5_THRESHOLD_HIGH, 0, 11),
  234. [F_THRESHOLD_MED] = REG_FIELD(BWMON_V5_THRESHOLD_MED, 0, 11),
  235. [F_THRESHOLD_LOW] = REG_FIELD(BWMON_V5_THRESHOLD_LOW, 0, 11),
  236. [F_ZONE_ACTIONS_ZONE0] = REG_FIELD(BWMON_V5_ZONE_ACTIONS, 0, 7),
  237. [F_ZONE_ACTIONS_ZONE1] = REG_FIELD(BWMON_V5_ZONE_ACTIONS, 8, 15),
  238. [F_ZONE_ACTIONS_ZONE2] = REG_FIELD(BWMON_V5_ZONE_ACTIONS, 16, 23),
  239. [F_ZONE_ACTIONS_ZONE3] = REG_FIELD(BWMON_V5_ZONE_ACTIONS, 24, 31),
  240. [F_THRESHOLD_COUNT_ZONE0] = REG_FIELD(BWMON_V5_THRESHOLD_COUNT, 0, 7),
  241. [F_THRESHOLD_COUNT_ZONE1] = REG_FIELD(BWMON_V5_THRESHOLD_COUNT, 8, 15),
  242. [F_THRESHOLD_COUNT_ZONE2] = REG_FIELD(BWMON_V5_THRESHOLD_COUNT, 16, 23),
  243. [F_THRESHOLD_COUNT_ZONE3] = REG_FIELD(BWMON_V5_THRESHOLD_COUNT, 24, 31),
  244. [F_ZONE0_MAX] = REG_FIELD(BWMON_V5_ZONE_MAX(0), 0, 11),
  245. [F_ZONE1_MAX] = REG_FIELD(BWMON_V5_ZONE_MAX(1), 0, 11),
  246. [F_ZONE2_MAX] = REG_FIELD(BWMON_V5_ZONE_MAX(2), 0, 11),
  247. [F_ZONE3_MAX] = REG_FIELD(BWMON_V5_ZONE_MAX(3), 0, 11),
  248. };
  249. static const struct regmap_range sdm845_llcc_bwmon_reg_noread_ranges[] = {
  250. regmap_reg_range(BWMON_V5_IRQ_CLEAR, BWMON_V5_IRQ_CLEAR),
  251. regmap_reg_range(BWMON_V5_CLEAR, BWMON_V5_CLEAR),
  252. };
  253. static const struct regmap_access_table sdm845_llcc_bwmon_reg_read_table = {
  254. .no_ranges = sdm845_llcc_bwmon_reg_noread_ranges,
  255. .n_no_ranges = ARRAY_SIZE(sdm845_llcc_bwmon_reg_noread_ranges),
  256. };
  257. static const struct regmap_range sdm845_llcc_bwmon_reg_volatile_ranges[] = {
  258. regmap_reg_range(BWMON_V5_IRQ_STATUS, BWMON_V5_IRQ_STATUS),
  259. regmap_reg_range(BWMON_V5_ZONE_MAX(0), BWMON_V5_ZONE_MAX(3)),
  260. };
  261. static const struct regmap_access_table sdm845_llcc_bwmon_reg_volatile_table = {
  262. .yes_ranges = sdm845_llcc_bwmon_reg_volatile_ranges,
  263. .n_yes_ranges = ARRAY_SIZE(sdm845_llcc_bwmon_reg_volatile_ranges),
  264. };
  265. /*
  266. * Fill the cache for non-readable registers only as rest does not really
  267. * matter and can be read from the device.
  268. */
  269. static const struct reg_default sdm845_llcc_bwmon_reg_defaults[] = {
  270. { BWMON_V5_IRQ_CLEAR, 0x0 },
  271. { BWMON_V5_CLEAR, 0x0 },
  272. };
  273. static const struct regmap_config sdm845_llcc_bwmon_regmap_cfg = {
  274. .reg_bits = 32,
  275. .reg_stride = 4,
  276. .val_bits = 32,
  277. /*
  278. * No concurrent access expected - driver has one interrupt handler,
  279. * regmap is not shared, no driver or user-space API.
  280. */
  281. .disable_locking = true,
  282. .rd_table = &sdm845_llcc_bwmon_reg_read_table,
  283. .volatile_table = &sdm845_llcc_bwmon_reg_volatile_table,
  284. .reg_defaults = sdm845_llcc_bwmon_reg_defaults,
  285. .num_reg_defaults = ARRAY_SIZE(sdm845_llcc_bwmon_reg_defaults),
  286. /*
  287. * Cache is necessary for using regmap fields with non-readable
  288. * registers.
  289. */
  290. .cache_type = REGCACHE_RBTREE,
  291. };
  292. static void bwmon_clear_counters(struct icc_bwmon *bwmon, bool clear_all)
  293. {
  294. unsigned int val = BWMON_CLEAR_CLEAR;
  295. if (clear_all)
  296. val |= BWMON_CLEAR_CLEAR_ALL;
  297. /*
  298. * Clear counters. The order and barriers are
  299. * important. Quoting downstream Qualcomm msm-4.9 tree:
  300. *
  301. * The counter clear and IRQ clear bits are not in the same 4KB
  302. * region. So, we need to make sure the counter clear is completed
  303. * before we try to clear the IRQ or do any other counter operations.
  304. */
  305. regmap_field_force_write(bwmon->regs[F_CLEAR], val);
  306. if (bwmon->data->quirks & BWMON_NEEDS_FORCE_CLEAR)
  307. regmap_field_force_write(bwmon->regs[F_CLEAR], 0);
  308. }
  309. static void bwmon_clear_irq(struct icc_bwmon *bwmon)
  310. {
  311. /*
  312. * Clear zone and global interrupts. The order and barriers are
  313. * important. Quoting downstream Qualcomm msm-4.9 tree:
  314. *
  315. * Synchronize the local interrupt clear in mon_irq_clear()
  316. * with the global interrupt clear here. Otherwise, the CPU
  317. * may reorder the two writes and clear the global interrupt
  318. * before the local interrupt, causing the global interrupt
  319. * to be retriggered by the local interrupt still being high.
  320. *
  321. * Similarly, because the global registers are in a different
  322. * region than the local registers, we need to ensure any register
  323. * writes to enable the monitor after this call are ordered with the
  324. * clearing here so that local writes don't happen before the
  325. * interrupt is cleared.
  326. */
  327. regmap_field_force_write(bwmon->regs[F_IRQ_CLEAR], BWMON_IRQ_ENABLE_MASK);
  328. if (bwmon->data->quirks & BWMON_NEEDS_FORCE_CLEAR)
  329. regmap_field_force_write(bwmon->regs[F_IRQ_CLEAR], 0);
  330. if (bwmon->data->quirks & BWMON_HAS_GLOBAL_IRQ)
  331. regmap_field_force_write(bwmon->regs[F_GLOBAL_IRQ_CLEAR],
  332. BWMON_V4_GLOBAL_IRQ_ENABLE_ENABLE);
  333. }
  334. static void bwmon_disable(struct icc_bwmon *bwmon)
  335. {
  336. /* Disable interrupts. Strict ordering, see bwmon_clear_irq(). */
  337. if (bwmon->data->quirks & BWMON_HAS_GLOBAL_IRQ)
  338. regmap_field_write(bwmon->regs[F_GLOBAL_IRQ_ENABLE], 0x0);
  339. regmap_field_write(bwmon->regs[F_IRQ_ENABLE], 0x0);
  340. /*
  341. * Disable bwmon. Must happen before bwmon_clear_irq() to avoid spurious
  342. * IRQ.
  343. */
  344. regmap_field_write(bwmon->regs[F_ENABLE], 0x0);
  345. }
  346. static void bwmon_enable(struct icc_bwmon *bwmon, unsigned int irq_enable)
  347. {
  348. /* Enable interrupts */
  349. if (bwmon->data->quirks & BWMON_HAS_GLOBAL_IRQ)
  350. regmap_field_write(bwmon->regs[F_GLOBAL_IRQ_ENABLE],
  351. BWMON_V4_GLOBAL_IRQ_ENABLE_ENABLE);
  352. regmap_field_write(bwmon->regs[F_IRQ_ENABLE], irq_enable);
  353. /* Enable bwmon */
  354. regmap_field_write(bwmon->regs[F_ENABLE], BWMON_ENABLE_ENABLE);
  355. }
  356. static unsigned int bwmon_kbps_to_count(struct icc_bwmon *bwmon,
  357. unsigned int kbps)
  358. {
  359. return kbps / bwmon->data->count_unit_kb;
  360. }
  361. static void bwmon_set_threshold(struct icc_bwmon *bwmon,
  362. struct regmap_field *reg, unsigned int kbps)
  363. {
  364. unsigned int thres;
  365. thres = mult_frac(bwmon_kbps_to_count(bwmon, kbps),
  366. bwmon->data->sample_ms, MSEC_PER_SEC);
  367. regmap_field_write(reg, thres);
  368. }
  369. static void bwmon_start(struct icc_bwmon *bwmon)
  370. {
  371. const struct icc_bwmon_data *data = bwmon->data;
  372. int window;
  373. bwmon_clear_counters(bwmon, true);
  374. window = mult_frac(bwmon->data->sample_ms, HW_TIMER_HZ, MSEC_PER_SEC);
  375. /* Maximum sampling window: 0xffffff for v4 and 0xfffff for v5 */
  376. regmap_field_write(bwmon->regs[F_SAMPLE_WINDOW], window);
  377. bwmon_set_threshold(bwmon, bwmon->regs[F_THRESHOLD_HIGH],
  378. data->default_highbw_kbps);
  379. bwmon_set_threshold(bwmon, bwmon->regs[F_THRESHOLD_MED],
  380. data->default_medbw_kbps);
  381. bwmon_set_threshold(bwmon, bwmon->regs[F_THRESHOLD_LOW],
  382. data->default_lowbw_kbps);
  383. regmap_field_write(bwmon->regs[F_THRESHOLD_COUNT_ZONE0],
  384. BWMON_THRESHOLD_COUNT_ZONE0_DEFAULT);
  385. regmap_field_write(bwmon->regs[F_THRESHOLD_COUNT_ZONE1],
  386. data->zone1_thres_count);
  387. regmap_field_write(bwmon->regs[F_THRESHOLD_COUNT_ZONE2],
  388. BWMON_THRESHOLD_COUNT_ZONE2_DEFAULT);
  389. regmap_field_write(bwmon->regs[F_THRESHOLD_COUNT_ZONE3],
  390. data->zone3_thres_count);
  391. regmap_field_write(bwmon->regs[F_ZONE_ACTIONS_ZONE0],
  392. BWMON_ZONE_ACTIONS_ZONE0);
  393. regmap_field_write(bwmon->regs[F_ZONE_ACTIONS_ZONE1],
  394. BWMON_ZONE_ACTIONS_ZONE1);
  395. regmap_field_write(bwmon->regs[F_ZONE_ACTIONS_ZONE2],
  396. BWMON_ZONE_ACTIONS_ZONE2);
  397. regmap_field_write(bwmon->regs[F_ZONE_ACTIONS_ZONE3],
  398. BWMON_ZONE_ACTIONS_ZONE3);
  399. bwmon_clear_irq(bwmon);
  400. bwmon_enable(bwmon, BWMON_IRQ_ENABLE_MASK);
  401. }
  402. static irqreturn_t bwmon_intr(int irq, void *dev_id)
  403. {
  404. struct icc_bwmon *bwmon = dev_id;
  405. unsigned int status, max;
  406. int zone;
  407. if (regmap_field_read(bwmon->regs[F_IRQ_STATUS], &status))
  408. return IRQ_NONE;
  409. status &= BWMON_IRQ_ENABLE_MASK;
  410. if (!status) {
  411. /*
  412. * Only zone 1 and zone 3 interrupts are enabled but zone 2
  413. * threshold could be hit and trigger interrupt even if not
  414. * enabled.
  415. * Such spurious interrupt might come with valuable max count or
  416. * not, so solution would be to always check all
  417. * BWMON_ZONE_MAX() registers to find the highest value.
  418. * Such case is currently ignored.
  419. */
  420. return IRQ_NONE;
  421. }
  422. bwmon_disable(bwmon);
  423. zone = get_bitmask_order(status) - 1;
  424. /*
  425. * Zone max bytes count register returns count units within sampling
  426. * window. Downstream kernel for BWMONv4 (called BWMON type 2 in
  427. * downstream) always increments the max bytes count by one.
  428. */
  429. if (regmap_field_read(bwmon->regs[F_ZONE0_MAX + zone], &max))
  430. return IRQ_NONE;
  431. max += 1;
  432. max *= bwmon->data->count_unit_kb;
  433. bwmon->target_kbps = mult_frac(max, MSEC_PER_SEC, bwmon->data->sample_ms);
  434. return IRQ_WAKE_THREAD;
  435. }
  436. static irqreturn_t bwmon_intr_thread(int irq, void *dev_id)
  437. {
  438. struct icc_bwmon *bwmon = dev_id;
  439. unsigned int irq_enable = 0;
  440. struct dev_pm_opp *opp, *target_opp;
  441. unsigned int bw_kbps, up_kbps, down_kbps;
  442. bw_kbps = bwmon->target_kbps;
  443. target_opp = dev_pm_opp_find_bw_ceil(bwmon->dev, &bw_kbps, 0);
  444. if (IS_ERR(target_opp) && PTR_ERR(target_opp) == -ERANGE)
  445. target_opp = dev_pm_opp_find_bw_floor(bwmon->dev, &bw_kbps, 0);
  446. bwmon->target_kbps = bw_kbps;
  447. bw_kbps--;
  448. opp = dev_pm_opp_find_bw_floor(bwmon->dev, &bw_kbps, 0);
  449. if (IS_ERR(opp) && PTR_ERR(opp) == -ERANGE)
  450. down_kbps = bwmon->target_kbps;
  451. else
  452. down_kbps = bw_kbps;
  453. up_kbps = bwmon->target_kbps + 1;
  454. if (bwmon->target_kbps >= bwmon->max_bw_kbps)
  455. irq_enable = BIT(1);
  456. else if (bwmon->target_kbps <= bwmon->min_bw_kbps)
  457. irq_enable = BIT(3);
  458. else
  459. irq_enable = BWMON_IRQ_ENABLE_MASK;
  460. bwmon_set_threshold(bwmon, bwmon->regs[F_THRESHOLD_HIGH],
  461. up_kbps);
  462. bwmon_set_threshold(bwmon, bwmon->regs[F_THRESHOLD_MED],
  463. down_kbps);
  464. bwmon_clear_counters(bwmon, false);
  465. bwmon_clear_irq(bwmon);
  466. bwmon_enable(bwmon, irq_enable);
  467. if (bwmon->target_kbps == bwmon->current_kbps)
  468. goto out;
  469. dev_pm_opp_set_opp(bwmon->dev, target_opp);
  470. bwmon->current_kbps = bwmon->target_kbps;
  471. out:
  472. dev_pm_opp_put(target_opp);
  473. if (!IS_ERR(opp))
  474. dev_pm_opp_put(opp);
  475. return IRQ_HANDLED;
  476. }
  477. static int bwmon_init_regmap(struct platform_device *pdev,
  478. struct icc_bwmon *bwmon)
  479. {
  480. struct device *dev = &pdev->dev;
  481. void __iomem *base;
  482. struct regmap *map;
  483. base = devm_platform_ioremap_resource(pdev, 0);
  484. if (IS_ERR(base))
  485. return dev_err_probe(dev, PTR_ERR(base),
  486. "failed to map bwmon registers\n");
  487. map = devm_regmap_init_mmio(dev, base, bwmon->data->regmap_cfg);
  488. if (IS_ERR(map))
  489. return dev_err_probe(dev, PTR_ERR(map),
  490. "failed to initialize regmap\n");
  491. BUILD_BUG_ON(ARRAY_SIZE(msm8998_bwmon_reg_fields) != F_NUM_FIELDS);
  492. BUILD_BUG_ON(ARRAY_SIZE(sdm845_llcc_bwmon_reg_fields) != F_NUM_FIELDS);
  493. return devm_regmap_field_bulk_alloc(dev, map, bwmon->regs,
  494. bwmon->data->regmap_fields,
  495. F_NUM_FIELDS);
  496. }
  497. static int bwmon_probe(struct platform_device *pdev)
  498. {
  499. struct device *dev = &pdev->dev;
  500. struct dev_pm_opp *opp;
  501. struct icc_bwmon *bwmon;
  502. int ret;
  503. bwmon = devm_kzalloc(dev, sizeof(*bwmon), GFP_KERNEL);
  504. if (!bwmon)
  505. return -ENOMEM;
  506. bwmon->data = of_device_get_match_data(dev);
  507. ret = bwmon_init_regmap(pdev, bwmon);
  508. if (ret)
  509. return ret;
  510. bwmon->irq = platform_get_irq(pdev, 0);
  511. if (bwmon->irq < 0)
  512. return bwmon->irq;
  513. ret = devm_pm_opp_of_add_table(dev);
  514. if (ret)
  515. return dev_err_probe(dev, ret, "failed to add OPP table\n");
  516. bwmon->max_bw_kbps = UINT_MAX;
  517. opp = dev_pm_opp_find_bw_floor(dev, &bwmon->max_bw_kbps, 0);
  518. if (IS_ERR(opp))
  519. return dev_err_probe(dev, PTR_ERR(opp), "failed to find max peak bandwidth\n");
  520. bwmon->min_bw_kbps = 0;
  521. opp = dev_pm_opp_find_bw_ceil(dev, &bwmon->min_bw_kbps, 0);
  522. if (IS_ERR(opp))
  523. return dev_err_probe(dev, PTR_ERR(opp), "failed to find min peak bandwidth\n");
  524. bwmon->dev = dev;
  525. bwmon_disable(bwmon);
  526. ret = devm_request_threaded_irq(dev, bwmon->irq, bwmon_intr,
  527. bwmon_intr_thread,
  528. IRQF_ONESHOT, dev_name(dev), bwmon);
  529. if (ret)
  530. return dev_err_probe(dev, ret, "failed to request IRQ\n");
  531. platform_set_drvdata(pdev, bwmon);
  532. bwmon_start(bwmon);
  533. return 0;
  534. }
  535. static int bwmon_remove(struct platform_device *pdev)
  536. {
  537. struct icc_bwmon *bwmon = platform_get_drvdata(pdev);
  538. bwmon_disable(bwmon);
  539. return 0;
  540. }
  541. static const struct icc_bwmon_data msm8998_bwmon_data = {
  542. .sample_ms = 4,
  543. .count_unit_kb = 64,
  544. .default_highbw_kbps = 4800 * 1024, /* 4.8 GBps */
  545. .default_medbw_kbps = 512 * 1024, /* 512 MBps */
  546. .default_lowbw_kbps = 0,
  547. .zone1_thres_count = 16,
  548. .zone3_thres_count = 1,
  549. .quirks = BWMON_HAS_GLOBAL_IRQ,
  550. .regmap_fields = msm8998_bwmon_reg_fields,
  551. .regmap_cfg = &msm8998_bwmon_regmap_cfg,
  552. };
  553. static const struct icc_bwmon_data sdm845_llcc_bwmon_data = {
  554. .sample_ms = 4,
  555. .count_unit_kb = 1024,
  556. .default_highbw_kbps = 800 * 1024, /* 800 MBps */
  557. .default_medbw_kbps = 256 * 1024, /* 256 MBps */
  558. .default_lowbw_kbps = 0,
  559. .zone1_thres_count = 16,
  560. .zone3_thres_count = 1,
  561. .regmap_fields = sdm845_llcc_bwmon_reg_fields,
  562. .regmap_cfg = &sdm845_llcc_bwmon_regmap_cfg,
  563. };
  564. static const struct icc_bwmon_data sc7280_llcc_bwmon_data = {
  565. .sample_ms = 4,
  566. .count_unit_kb = 64,
  567. .default_highbw_kbps = 800 * 1024, /* 800 MBps */
  568. .default_medbw_kbps = 256 * 1024, /* 256 MBps */
  569. .default_lowbw_kbps = 0,
  570. .zone1_thres_count = 16,
  571. .zone3_thres_count = 1,
  572. .quirks = BWMON_NEEDS_FORCE_CLEAR,
  573. .regmap_fields = sdm845_llcc_bwmon_reg_fields,
  574. .regmap_cfg = &sdm845_llcc_bwmon_regmap_cfg,
  575. };
  576. static const struct of_device_id bwmon_of_match[] = {
  577. {
  578. .compatible = "qcom,msm8998-bwmon",
  579. .data = &msm8998_bwmon_data
  580. }, {
  581. .compatible = "qcom,sdm845-llcc-bwmon",
  582. .data = &sdm845_llcc_bwmon_data
  583. }, {
  584. .compatible = "qcom,sc7280-llcc-bwmon",
  585. .data = &sc7280_llcc_bwmon_data
  586. },
  587. {}
  588. };
  589. MODULE_DEVICE_TABLE(of, bwmon_of_match);
  590. static struct platform_driver bwmon_driver = {
  591. .probe = bwmon_probe,
  592. .remove = bwmon_remove,
  593. .driver = {
  594. .name = "qcom-bwmon",
  595. .of_match_table = bwmon_of_match,
  596. },
  597. };
  598. module_platform_driver(bwmon_driver);
  599. MODULE_AUTHOR("Krzysztof Kozlowski <[email protected]>");
  600. MODULE_DESCRIPTION("QCOM BWMON driver");
  601. MODULE_LICENSE("GPL");