llcc-slice.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  4. *
  5. */
  6. #include <linux/bitmap.h>
  7. #include <linux/bitops.h>
  8. #include <linux/device.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/of_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/sizes.h>
  16. #include <linux/slab.h>
  17. #include <linux/soc/qcom/llcc-qcom.h>
  18. #define ACTIVATE BIT(0)
  19. #define DEACTIVATE BIT(1)
  20. #define ACT_CTRL_OPCODE_ACTIVATE BIT(0)
  21. #define ACT_CTRL_OPCODE_DEACTIVATE BIT(1)
  22. #define ACT_CTRL_ACT_TRIG BIT(0)
  23. #define ACT_CTRL_OPCODE_SHIFT 0x01
  24. #define ATTR1_PROBE_TARGET_WAYS_SHIFT 0x02
  25. #define ATTR1_FIXED_SIZE_SHIFT 0x03
  26. #define ATTR1_PRIORITY_SHIFT 0x04
  27. #define ATTR1_MAX_CAP_SHIFT 0x10
  28. #define ATTR0_RES_WAYS_MASK GENMASK(11, 0)
  29. #define ATTR0_BONUS_WAYS_MASK GENMASK(27, 16)
  30. #define ATTR0_BONUS_WAYS_SHIFT 0x10
  31. #define LLCC_STATUS_READ_DELAY 100
  32. #define CACHE_LINE_SIZE_SHIFT 6
  33. #define LLCC_COMMON_STATUS0 0x0003000c
  34. #define LLCC_LB_CNT_MASK GENMASK(31, 28)
  35. #define LLCC_LB_CNT_SHIFT 28
  36. #define MAX_CAP_TO_BYTES(n) (n * SZ_1K)
  37. #define LLCC_TRP_ACT_CTRLn(n) (n * SZ_4K)
  38. #define LLCC_TRP_STATUSn(n) (4 + n * SZ_4K)
  39. #define LLCC_TRP_ATTR0_CFGn(n) (0x21000 + SZ_8 * n)
  40. #define LLCC_TRP_ATTR1_CFGn(n) (0x21004 + SZ_8 * n)
  41. #define BANK_OFFSET_STRIDE 0x80000
  42. static struct llcc_drv_data *drv_data = (void *) -EPROBE_DEFER;
  43. static struct regmap_config llcc_regmap_config = {
  44. .reg_bits = 32,
  45. .reg_stride = 4,
  46. .val_bits = 32,
  47. .fast_io = true,
  48. };
  49. /**
  50. * llcc_slice_getd - get llcc slice descriptor
  51. * @uid: usecase_id for the client
  52. *
  53. * A pointer to llcc slice descriptor will be returned on success and
  54. * and error pointer is returned on failure
  55. */
  56. struct llcc_slice_desc *llcc_slice_getd(u32 uid)
  57. {
  58. const struct llcc_slice_config *cfg;
  59. struct llcc_slice_desc *desc;
  60. u32 sz, count;
  61. if (IS_ERR(drv_data))
  62. return ERR_CAST(drv_data);
  63. cfg = drv_data->cfg;
  64. sz = drv_data->cfg_size;
  65. for (count = 0; cfg && count < sz; count++, cfg++)
  66. if (cfg->usecase_id == uid)
  67. break;
  68. if (count == sz || !cfg)
  69. return ERR_PTR(-ENODEV);
  70. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  71. if (!desc)
  72. return ERR_PTR(-ENOMEM);
  73. desc->slice_id = cfg->slice_id;
  74. desc->slice_size = cfg->max_cap;
  75. return desc;
  76. }
  77. EXPORT_SYMBOL_GPL(llcc_slice_getd);
  78. /**
  79. * llcc_slice_putd - llcc slice descritpor
  80. * @desc: Pointer to llcc slice descriptor
  81. */
  82. void llcc_slice_putd(struct llcc_slice_desc *desc)
  83. {
  84. if (!IS_ERR_OR_NULL(desc))
  85. kfree(desc);
  86. }
  87. EXPORT_SYMBOL_GPL(llcc_slice_putd);
  88. static int llcc_update_act_ctrl(u32 sid,
  89. u32 act_ctrl_reg_val, u32 status)
  90. {
  91. u32 act_ctrl_reg;
  92. u32 status_reg;
  93. u32 slice_status;
  94. int ret;
  95. if (IS_ERR(drv_data))
  96. return PTR_ERR(drv_data);
  97. act_ctrl_reg = LLCC_TRP_ACT_CTRLn(sid);
  98. status_reg = LLCC_TRP_STATUSn(sid);
  99. /* Set the ACTIVE trigger */
  100. act_ctrl_reg_val |= ACT_CTRL_ACT_TRIG;
  101. ret = regmap_write(drv_data->bcast_regmap, act_ctrl_reg,
  102. act_ctrl_reg_val);
  103. if (ret)
  104. return ret;
  105. /* Clear the ACTIVE trigger */
  106. act_ctrl_reg_val &= ~ACT_CTRL_ACT_TRIG;
  107. ret = regmap_write(drv_data->bcast_regmap, act_ctrl_reg,
  108. act_ctrl_reg_val);
  109. if (ret)
  110. return ret;
  111. ret = regmap_read_poll_timeout(drv_data->bcast_regmap, status_reg,
  112. slice_status, !(slice_status & status),
  113. 0, LLCC_STATUS_READ_DELAY);
  114. return ret;
  115. }
  116. /**
  117. * llcc_slice_activate - Activate the llcc slice
  118. * @desc: Pointer to llcc slice descriptor
  119. *
  120. * A value of zero will be returned on success and a negative errno will
  121. * be returned in error cases
  122. */
  123. int llcc_slice_activate(struct llcc_slice_desc *desc)
  124. {
  125. int ret;
  126. u32 act_ctrl_val;
  127. if (IS_ERR(drv_data))
  128. return PTR_ERR(drv_data);
  129. if (IS_ERR_OR_NULL(desc))
  130. return -EINVAL;
  131. mutex_lock(&drv_data->lock);
  132. if (test_bit(desc->slice_id, drv_data->bitmap)) {
  133. mutex_unlock(&drv_data->lock);
  134. return 0;
  135. }
  136. act_ctrl_val = ACT_CTRL_OPCODE_ACTIVATE << ACT_CTRL_OPCODE_SHIFT;
  137. ret = llcc_update_act_ctrl(desc->slice_id, act_ctrl_val,
  138. DEACTIVATE);
  139. if (ret) {
  140. mutex_unlock(&drv_data->lock);
  141. return ret;
  142. }
  143. __set_bit(desc->slice_id, drv_data->bitmap);
  144. mutex_unlock(&drv_data->lock);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(llcc_slice_activate);
  148. /**
  149. * llcc_slice_deactivate - Deactivate the llcc slice
  150. * @desc: Pointer to llcc slice descriptor
  151. *
  152. * A value of zero will be returned on success and a negative errno will
  153. * be returned in error cases
  154. */
  155. int llcc_slice_deactivate(struct llcc_slice_desc *desc)
  156. {
  157. u32 act_ctrl_val;
  158. int ret;
  159. if (IS_ERR(drv_data))
  160. return PTR_ERR(drv_data);
  161. if (IS_ERR_OR_NULL(desc))
  162. return -EINVAL;
  163. mutex_lock(&drv_data->lock);
  164. if (!test_bit(desc->slice_id, drv_data->bitmap)) {
  165. mutex_unlock(&drv_data->lock);
  166. return 0;
  167. }
  168. act_ctrl_val = ACT_CTRL_OPCODE_DEACTIVATE << ACT_CTRL_OPCODE_SHIFT;
  169. ret = llcc_update_act_ctrl(desc->slice_id, act_ctrl_val,
  170. ACTIVATE);
  171. if (ret) {
  172. mutex_unlock(&drv_data->lock);
  173. return ret;
  174. }
  175. __clear_bit(desc->slice_id, drv_data->bitmap);
  176. mutex_unlock(&drv_data->lock);
  177. return ret;
  178. }
  179. EXPORT_SYMBOL_GPL(llcc_slice_deactivate);
  180. /**
  181. * llcc_get_slice_id - return the slice id
  182. * @desc: Pointer to llcc slice descriptor
  183. */
  184. int llcc_get_slice_id(struct llcc_slice_desc *desc)
  185. {
  186. if (IS_ERR_OR_NULL(desc))
  187. return -EINVAL;
  188. return desc->slice_id;
  189. }
  190. EXPORT_SYMBOL_GPL(llcc_get_slice_id);
  191. /**
  192. * llcc_get_slice_size - return the slice id
  193. * @desc: Pointer to llcc slice descriptor
  194. */
  195. size_t llcc_get_slice_size(struct llcc_slice_desc *desc)
  196. {
  197. if (IS_ERR_OR_NULL(desc))
  198. return 0;
  199. return desc->slice_size;
  200. }
  201. EXPORT_SYMBOL_GPL(llcc_get_slice_size);
  202. static int qcom_llcc_cfg_program(struct platform_device *pdev)
  203. {
  204. int i;
  205. u32 attr1_cfg;
  206. u32 attr0_cfg;
  207. u32 attr1_val;
  208. u32 attr0_val;
  209. u32 max_cap_cacheline;
  210. u32 sz;
  211. int ret = 0;
  212. const struct llcc_slice_config *llcc_table;
  213. struct llcc_slice_desc desc;
  214. sz = drv_data->cfg_size;
  215. llcc_table = drv_data->cfg;
  216. for (i = 0; i < sz; i++) {
  217. attr1_cfg = LLCC_TRP_ATTR1_CFGn(llcc_table[i].slice_id);
  218. attr0_cfg = LLCC_TRP_ATTR0_CFGn(llcc_table[i].slice_id);
  219. attr1_val = llcc_table[i].cache_mode;
  220. attr1_val |= llcc_table[i].probe_target_ways <<
  221. ATTR1_PROBE_TARGET_WAYS_SHIFT;
  222. attr1_val |= llcc_table[i].fixed_size <<
  223. ATTR1_FIXED_SIZE_SHIFT;
  224. attr1_val |= llcc_table[i].priority <<
  225. ATTR1_PRIORITY_SHIFT;
  226. max_cap_cacheline = MAX_CAP_TO_BYTES(llcc_table[i].max_cap);
  227. /* LLCC instances can vary for each target.
  228. * The SW writes to broadcast register which gets propagated
  229. * to each llcc instace (llcc0,.. llccN).
  230. * Since the size of the memory is divided equally amongst the
  231. * llcc instances, we need to configure the max cap accordingly.
  232. */
  233. max_cap_cacheline = max_cap_cacheline / drv_data->num_banks;
  234. max_cap_cacheline >>= CACHE_LINE_SIZE_SHIFT;
  235. attr1_val |= max_cap_cacheline << ATTR1_MAX_CAP_SHIFT;
  236. attr0_val = llcc_table[i].res_ways & ATTR0_RES_WAYS_MASK;
  237. attr0_val |= llcc_table[i].bonus_ways << ATTR0_BONUS_WAYS_SHIFT;
  238. ret = regmap_write(drv_data->bcast_regmap, attr1_cfg,
  239. attr1_val);
  240. if (ret)
  241. return ret;
  242. ret = regmap_write(drv_data->bcast_regmap, attr0_cfg,
  243. attr0_val);
  244. if (ret)
  245. return ret;
  246. if (llcc_table[i].activate_on_init) {
  247. desc.slice_id = llcc_table[i].slice_id;
  248. ret = llcc_slice_activate(&desc);
  249. }
  250. }
  251. return ret;
  252. }
  253. int qcom_llcc_remove(struct platform_device *pdev)
  254. {
  255. /* Set the global pointer to a error code to avoid referencing it */
  256. drv_data = ERR_PTR(-ENODEV);
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(qcom_llcc_remove);
  260. static struct regmap *qcom_llcc_init_mmio(struct platform_device *pdev,
  261. const char *name)
  262. {
  263. struct resource *res;
  264. void __iomem *base;
  265. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  266. if (!res)
  267. return ERR_PTR(-ENODEV);
  268. base = devm_ioremap_resource(&pdev->dev, res);
  269. if (IS_ERR(base))
  270. return ERR_CAST(base);
  271. llcc_regmap_config.name = name;
  272. return devm_regmap_init_mmio(&pdev->dev, base, &llcc_regmap_config);
  273. }
  274. int qcom_llcc_probe(struct platform_device *pdev,
  275. const struct llcc_slice_config *llcc_cfg, u32 sz)
  276. {
  277. u32 num_banks;
  278. struct device *dev = &pdev->dev;
  279. int ret, i;
  280. struct platform_device *llcc_edac;
  281. drv_data = devm_kzalloc(dev, sizeof(*drv_data), GFP_KERNEL);
  282. if (!drv_data) {
  283. ret = -ENOMEM;
  284. goto err;
  285. }
  286. drv_data->regmap = qcom_llcc_init_mmio(pdev, "llcc_base");
  287. if (IS_ERR(drv_data->regmap)) {
  288. ret = PTR_ERR(drv_data->regmap);
  289. goto err;
  290. }
  291. drv_data->bcast_regmap =
  292. qcom_llcc_init_mmio(pdev, "llcc_broadcast_base");
  293. if (IS_ERR(drv_data->bcast_regmap)) {
  294. ret = PTR_ERR(drv_data->bcast_regmap);
  295. goto err;
  296. }
  297. ret = regmap_read(drv_data->regmap, LLCC_COMMON_STATUS0,
  298. &num_banks);
  299. if (ret)
  300. goto err;
  301. num_banks &= LLCC_LB_CNT_MASK;
  302. num_banks >>= LLCC_LB_CNT_SHIFT;
  303. drv_data->num_banks = num_banks;
  304. for (i = 0; i < sz; i++)
  305. if (llcc_cfg[i].slice_id > drv_data->max_slices)
  306. drv_data->max_slices = llcc_cfg[i].slice_id;
  307. drv_data->offsets = devm_kcalloc(dev, num_banks, sizeof(u32),
  308. GFP_KERNEL);
  309. if (!drv_data->offsets) {
  310. ret = -ENOMEM;
  311. goto err;
  312. }
  313. for (i = 0; i < num_banks; i++)
  314. drv_data->offsets[i] = i * BANK_OFFSET_STRIDE;
  315. drv_data->bitmap = devm_kcalloc(dev,
  316. BITS_TO_LONGS(drv_data->max_slices), sizeof(unsigned long),
  317. GFP_KERNEL);
  318. if (!drv_data->bitmap) {
  319. ret = -ENOMEM;
  320. goto err;
  321. }
  322. drv_data->cfg = llcc_cfg;
  323. drv_data->cfg_size = sz;
  324. mutex_init(&drv_data->lock);
  325. platform_set_drvdata(pdev, drv_data);
  326. ret = qcom_llcc_cfg_program(pdev);
  327. if (ret)
  328. goto err;
  329. drv_data->ecc_irq = platform_get_irq(pdev, 0);
  330. if (drv_data->ecc_irq >= 0) {
  331. llcc_edac = platform_device_register_data(&pdev->dev,
  332. "qcom_llcc_edac", -1, drv_data,
  333. sizeof(*drv_data));
  334. if (IS_ERR(llcc_edac))
  335. dev_err(dev, "Failed to register llcc edac driver\n");
  336. }
  337. return 0;
  338. err:
  339. drv_data = ERR_PTR(-ENODEV);
  340. return ret;
  341. }
  342. EXPORT_SYMBOL_GPL(qcom_llcc_probe);
  343. MODULE_LICENSE("GPL v2");
  344. MODULE_DESCRIPTION("Qualcomm Last Level Cache Controller");