spectrum1_kvdl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
  3. #include <linux/kernel.h>
  4. #include <linux/bitops.h>
  5. #include "spectrum.h"
  6. #define MLXSW_SP1_KVDL_SINGLE_BASE 0
  7. #define MLXSW_SP1_KVDL_SINGLE_SIZE 16384
  8. #define MLXSW_SP1_KVDL_SINGLE_END \
  9. (MLXSW_SP1_KVDL_SINGLE_SIZE + MLXSW_SP1_KVDL_SINGLE_BASE - 1)
  10. #define MLXSW_SP1_KVDL_CHUNKS_BASE \
  11. (MLXSW_SP1_KVDL_SINGLE_BASE + MLXSW_SP1_KVDL_SINGLE_SIZE)
  12. #define MLXSW_SP1_KVDL_CHUNKS_SIZE 49152
  13. #define MLXSW_SP1_KVDL_CHUNKS_END \
  14. (MLXSW_SP1_KVDL_CHUNKS_SIZE + MLXSW_SP1_KVDL_CHUNKS_BASE - 1)
  15. #define MLXSW_SP1_KVDL_LARGE_CHUNKS_BASE \
  16. (MLXSW_SP1_KVDL_CHUNKS_BASE + MLXSW_SP1_KVDL_CHUNKS_SIZE)
  17. #define MLXSW_SP1_KVDL_LARGE_CHUNKS_SIZE \
  18. (MLXSW_SP_KVD_LINEAR_SIZE - MLXSW_SP1_KVDL_LARGE_CHUNKS_BASE)
  19. #define MLXSW_SP1_KVDL_LARGE_CHUNKS_END \
  20. (MLXSW_SP1_KVDL_LARGE_CHUNKS_SIZE + MLXSW_SP1_KVDL_LARGE_CHUNKS_BASE - 1)
  21. #define MLXSW_SP1_KVDL_SINGLE_ALLOC_SIZE 1
  22. #define MLXSW_SP1_KVDL_CHUNKS_ALLOC_SIZE 32
  23. #define MLXSW_SP1_KVDL_LARGE_CHUNKS_ALLOC_SIZE 512
  24. struct mlxsw_sp1_kvdl_part_info {
  25. unsigned int part_index;
  26. unsigned int start_index;
  27. unsigned int end_index;
  28. unsigned int alloc_size;
  29. enum mlxsw_sp_resource_id resource_id;
  30. };
  31. enum mlxsw_sp1_kvdl_part_id {
  32. MLXSW_SP1_KVDL_PART_ID_SINGLE,
  33. MLXSW_SP1_KVDL_PART_ID_CHUNKS,
  34. MLXSW_SP1_KVDL_PART_ID_LARGE_CHUNKS,
  35. };
  36. #define MLXSW_SP1_KVDL_PART_INFO(id) \
  37. [MLXSW_SP1_KVDL_PART_ID_##id] = { \
  38. .start_index = MLXSW_SP1_KVDL_##id##_BASE, \
  39. .end_index = MLXSW_SP1_KVDL_##id##_END, \
  40. .alloc_size = MLXSW_SP1_KVDL_##id##_ALLOC_SIZE, \
  41. .resource_id = MLXSW_SP_RESOURCE_KVD_LINEAR_##id, \
  42. }
  43. static const struct mlxsw_sp1_kvdl_part_info mlxsw_sp1_kvdl_parts_info[] = {
  44. MLXSW_SP1_KVDL_PART_INFO(SINGLE),
  45. MLXSW_SP1_KVDL_PART_INFO(CHUNKS),
  46. MLXSW_SP1_KVDL_PART_INFO(LARGE_CHUNKS),
  47. };
  48. #define MLXSW_SP1_KVDL_PARTS_INFO_LEN ARRAY_SIZE(mlxsw_sp1_kvdl_parts_info)
  49. struct mlxsw_sp1_kvdl_part {
  50. struct mlxsw_sp1_kvdl_part_info info;
  51. unsigned long usage[0]; /* Entries */
  52. };
  53. struct mlxsw_sp1_kvdl {
  54. struct mlxsw_sp1_kvdl_part *parts[MLXSW_SP1_KVDL_PARTS_INFO_LEN];
  55. };
  56. static struct mlxsw_sp1_kvdl_part *
  57. mlxsw_sp1_kvdl_alloc_size_part(struct mlxsw_sp1_kvdl *kvdl,
  58. unsigned int alloc_size)
  59. {
  60. struct mlxsw_sp1_kvdl_part *part, *min_part = NULL;
  61. int i;
  62. for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++) {
  63. part = kvdl->parts[i];
  64. if (alloc_size <= part->info.alloc_size &&
  65. (!min_part ||
  66. part->info.alloc_size <= min_part->info.alloc_size))
  67. min_part = part;
  68. }
  69. return min_part ?: ERR_PTR(-ENOBUFS);
  70. }
  71. static struct mlxsw_sp1_kvdl_part *
  72. mlxsw_sp1_kvdl_index_part(struct mlxsw_sp1_kvdl *kvdl, u32 kvdl_index)
  73. {
  74. struct mlxsw_sp1_kvdl_part *part;
  75. int i;
  76. for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++) {
  77. part = kvdl->parts[i];
  78. if (kvdl_index >= part->info.start_index &&
  79. kvdl_index <= part->info.end_index)
  80. return part;
  81. }
  82. return ERR_PTR(-EINVAL);
  83. }
  84. static u32
  85. mlxsw_sp1_kvdl_to_kvdl_index(const struct mlxsw_sp1_kvdl_part_info *info,
  86. unsigned int entry_index)
  87. {
  88. return info->start_index + entry_index * info->alloc_size;
  89. }
  90. static unsigned int
  91. mlxsw_sp1_kvdl_to_entry_index(const struct mlxsw_sp1_kvdl_part_info *info,
  92. u32 kvdl_index)
  93. {
  94. return (kvdl_index - info->start_index) / info->alloc_size;
  95. }
  96. static int mlxsw_sp1_kvdl_part_alloc(struct mlxsw_sp1_kvdl_part *part,
  97. u32 *p_kvdl_index)
  98. {
  99. const struct mlxsw_sp1_kvdl_part_info *info = &part->info;
  100. unsigned int entry_index, nr_entries;
  101. nr_entries = (info->end_index - info->start_index + 1) /
  102. info->alloc_size;
  103. entry_index = find_first_zero_bit(part->usage, nr_entries);
  104. if (entry_index == nr_entries)
  105. return -ENOBUFS;
  106. __set_bit(entry_index, part->usage);
  107. *p_kvdl_index = mlxsw_sp1_kvdl_to_kvdl_index(info, entry_index);
  108. return 0;
  109. }
  110. static void mlxsw_sp1_kvdl_part_free(struct mlxsw_sp1_kvdl_part *part,
  111. u32 kvdl_index)
  112. {
  113. const struct mlxsw_sp1_kvdl_part_info *info = &part->info;
  114. unsigned int entry_index;
  115. entry_index = mlxsw_sp1_kvdl_to_entry_index(info, kvdl_index);
  116. __clear_bit(entry_index, part->usage);
  117. }
  118. static int mlxsw_sp1_kvdl_alloc(struct mlxsw_sp *mlxsw_sp, void *priv,
  119. enum mlxsw_sp_kvdl_entry_type type,
  120. unsigned int entry_count,
  121. u32 *p_entry_index)
  122. {
  123. struct mlxsw_sp1_kvdl *kvdl = priv;
  124. struct mlxsw_sp1_kvdl_part *part;
  125. /* Find partition with smallest allocation size satisfying the
  126. * requested size.
  127. */
  128. part = mlxsw_sp1_kvdl_alloc_size_part(kvdl, entry_count);
  129. if (IS_ERR(part))
  130. return PTR_ERR(part);
  131. return mlxsw_sp1_kvdl_part_alloc(part, p_entry_index);
  132. }
  133. static void mlxsw_sp1_kvdl_free(struct mlxsw_sp *mlxsw_sp, void *priv,
  134. enum mlxsw_sp_kvdl_entry_type type,
  135. unsigned int entry_count, int entry_index)
  136. {
  137. struct mlxsw_sp1_kvdl *kvdl = priv;
  138. struct mlxsw_sp1_kvdl_part *part;
  139. part = mlxsw_sp1_kvdl_index_part(kvdl, entry_index);
  140. if (IS_ERR(part))
  141. return;
  142. mlxsw_sp1_kvdl_part_free(part, entry_index);
  143. }
  144. static int mlxsw_sp1_kvdl_alloc_size_query(struct mlxsw_sp *mlxsw_sp,
  145. void *priv,
  146. enum mlxsw_sp_kvdl_entry_type type,
  147. unsigned int entry_count,
  148. unsigned int *p_alloc_size)
  149. {
  150. struct mlxsw_sp1_kvdl *kvdl = priv;
  151. struct mlxsw_sp1_kvdl_part *part;
  152. part = mlxsw_sp1_kvdl_alloc_size_part(kvdl, entry_count);
  153. if (IS_ERR(part))
  154. return PTR_ERR(part);
  155. *p_alloc_size = part->info.alloc_size;
  156. return 0;
  157. }
  158. static void mlxsw_sp1_kvdl_part_update(struct mlxsw_sp1_kvdl_part *part,
  159. struct mlxsw_sp1_kvdl_part *part_prev,
  160. unsigned int size)
  161. {
  162. if (!part_prev) {
  163. part->info.end_index = size - 1;
  164. } else {
  165. part->info.start_index = part_prev->info.end_index + 1;
  166. part->info.end_index = part->info.start_index + size - 1;
  167. }
  168. }
  169. static struct mlxsw_sp1_kvdl_part *
  170. mlxsw_sp1_kvdl_part_init(struct mlxsw_sp *mlxsw_sp,
  171. const struct mlxsw_sp1_kvdl_part_info *info,
  172. struct mlxsw_sp1_kvdl_part *part_prev)
  173. {
  174. struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
  175. struct mlxsw_sp1_kvdl_part *part;
  176. bool need_update = true;
  177. unsigned int nr_entries;
  178. size_t usage_size;
  179. u64 resource_size;
  180. int err;
  181. err = devlink_resource_size_get(devlink, info->resource_id,
  182. &resource_size);
  183. if (err) {
  184. need_update = false;
  185. resource_size = info->end_index - info->start_index + 1;
  186. }
  187. nr_entries = div_u64(resource_size, info->alloc_size);
  188. usage_size = BITS_TO_LONGS(nr_entries) * sizeof(unsigned long);
  189. part = kzalloc(sizeof(*part) + usage_size, GFP_KERNEL);
  190. if (!part)
  191. return ERR_PTR(-ENOMEM);
  192. memcpy(&part->info, info, sizeof(part->info));
  193. if (need_update)
  194. mlxsw_sp1_kvdl_part_update(part, part_prev, resource_size);
  195. return part;
  196. }
  197. static void mlxsw_sp1_kvdl_part_fini(struct mlxsw_sp1_kvdl_part *part)
  198. {
  199. kfree(part);
  200. }
  201. static int mlxsw_sp1_kvdl_parts_init(struct mlxsw_sp *mlxsw_sp,
  202. struct mlxsw_sp1_kvdl *kvdl)
  203. {
  204. const struct mlxsw_sp1_kvdl_part_info *info;
  205. struct mlxsw_sp1_kvdl_part *part_prev = NULL;
  206. int err, i;
  207. for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++) {
  208. info = &mlxsw_sp1_kvdl_parts_info[i];
  209. kvdl->parts[i] = mlxsw_sp1_kvdl_part_init(mlxsw_sp, info,
  210. part_prev);
  211. if (IS_ERR(kvdl->parts[i])) {
  212. err = PTR_ERR(kvdl->parts[i]);
  213. goto err_kvdl_part_init;
  214. }
  215. part_prev = kvdl->parts[i];
  216. }
  217. return 0;
  218. err_kvdl_part_init:
  219. for (i--; i >= 0; i--)
  220. mlxsw_sp1_kvdl_part_fini(kvdl->parts[i]);
  221. return err;
  222. }
  223. static void mlxsw_sp1_kvdl_parts_fini(struct mlxsw_sp1_kvdl *kvdl)
  224. {
  225. int i;
  226. for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++)
  227. mlxsw_sp1_kvdl_part_fini(kvdl->parts[i]);
  228. }
  229. static u64 mlxsw_sp1_kvdl_part_occ(struct mlxsw_sp1_kvdl_part *part)
  230. {
  231. const struct mlxsw_sp1_kvdl_part_info *info = &part->info;
  232. unsigned int nr_entries;
  233. int bit = -1;
  234. u64 occ = 0;
  235. nr_entries = (info->end_index -
  236. info->start_index + 1) /
  237. info->alloc_size;
  238. while ((bit = find_next_bit(part->usage, nr_entries, bit + 1))
  239. < nr_entries)
  240. occ += info->alloc_size;
  241. return occ;
  242. }
  243. static u64 mlxsw_sp1_kvdl_occ_get(void *priv)
  244. {
  245. const struct mlxsw_sp1_kvdl *kvdl = priv;
  246. u64 occ = 0;
  247. int i;
  248. for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++)
  249. occ += mlxsw_sp1_kvdl_part_occ(kvdl->parts[i]);
  250. return occ;
  251. }
  252. static u64 mlxsw_sp1_kvdl_single_occ_get(void *priv)
  253. {
  254. const struct mlxsw_sp1_kvdl *kvdl = priv;
  255. struct mlxsw_sp1_kvdl_part *part;
  256. part = kvdl->parts[MLXSW_SP1_KVDL_PART_ID_SINGLE];
  257. return mlxsw_sp1_kvdl_part_occ(part);
  258. }
  259. static u64 mlxsw_sp1_kvdl_chunks_occ_get(void *priv)
  260. {
  261. const struct mlxsw_sp1_kvdl *kvdl = priv;
  262. struct mlxsw_sp1_kvdl_part *part;
  263. part = kvdl->parts[MLXSW_SP1_KVDL_PART_ID_CHUNKS];
  264. return mlxsw_sp1_kvdl_part_occ(part);
  265. }
  266. static u64 mlxsw_sp1_kvdl_large_chunks_occ_get(void *priv)
  267. {
  268. const struct mlxsw_sp1_kvdl *kvdl = priv;
  269. struct mlxsw_sp1_kvdl_part *part;
  270. part = kvdl->parts[MLXSW_SP1_KVDL_PART_ID_LARGE_CHUNKS];
  271. return mlxsw_sp1_kvdl_part_occ(part);
  272. }
  273. static int mlxsw_sp1_kvdl_init(struct mlxsw_sp *mlxsw_sp, void *priv)
  274. {
  275. struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
  276. struct mlxsw_sp1_kvdl *kvdl = priv;
  277. int err;
  278. err = mlxsw_sp1_kvdl_parts_init(mlxsw_sp, kvdl);
  279. if (err)
  280. return err;
  281. devlink_resource_occ_get_register(devlink,
  282. MLXSW_SP_RESOURCE_KVD_LINEAR,
  283. mlxsw_sp1_kvdl_occ_get,
  284. kvdl);
  285. devlink_resource_occ_get_register(devlink,
  286. MLXSW_SP_RESOURCE_KVD_LINEAR_SINGLE,
  287. mlxsw_sp1_kvdl_single_occ_get,
  288. kvdl);
  289. devlink_resource_occ_get_register(devlink,
  290. MLXSW_SP_RESOURCE_KVD_LINEAR_CHUNKS,
  291. mlxsw_sp1_kvdl_chunks_occ_get,
  292. kvdl);
  293. devlink_resource_occ_get_register(devlink,
  294. MLXSW_SP_RESOURCE_KVD_LINEAR_LARGE_CHUNKS,
  295. mlxsw_sp1_kvdl_large_chunks_occ_get,
  296. kvdl);
  297. return 0;
  298. }
  299. static void mlxsw_sp1_kvdl_fini(struct mlxsw_sp *mlxsw_sp, void *priv)
  300. {
  301. struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
  302. struct mlxsw_sp1_kvdl *kvdl = priv;
  303. devlink_resource_occ_get_unregister(devlink,
  304. MLXSW_SP_RESOURCE_KVD_LINEAR_LARGE_CHUNKS);
  305. devlink_resource_occ_get_unregister(devlink,
  306. MLXSW_SP_RESOURCE_KVD_LINEAR_CHUNKS);
  307. devlink_resource_occ_get_unregister(devlink,
  308. MLXSW_SP_RESOURCE_KVD_LINEAR_SINGLE);
  309. devlink_resource_occ_get_unregister(devlink,
  310. MLXSW_SP_RESOURCE_KVD_LINEAR);
  311. mlxsw_sp1_kvdl_parts_fini(kvdl);
  312. }
  313. const struct mlxsw_sp_kvdl_ops mlxsw_sp1_kvdl_ops = {
  314. .priv_size = sizeof(struct mlxsw_sp1_kvdl),
  315. .init = mlxsw_sp1_kvdl_init,
  316. .fini = mlxsw_sp1_kvdl_fini,
  317. .alloc = mlxsw_sp1_kvdl_alloc,
  318. .free = mlxsw_sp1_kvdl_free,
  319. .alloc_size_query = mlxsw_sp1_kvdl_alloc_size_query,
  320. };
  321. int mlxsw_sp1_kvdl_resources_register(struct mlxsw_core *mlxsw_core)
  322. {
  323. struct devlink *devlink = priv_to_devlink(mlxsw_core);
  324. static struct devlink_resource_size_params size_params;
  325. u32 kvdl_max_size;
  326. int err;
  327. kvdl_max_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
  328. MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE) -
  329. MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE);
  330. devlink_resource_size_params_init(&size_params, 0, kvdl_max_size,
  331. MLXSW_SP1_KVDL_SINGLE_ALLOC_SIZE,
  332. DEVLINK_RESOURCE_UNIT_ENTRY);
  333. err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR_SINGLES,
  334. MLXSW_SP1_KVDL_SINGLE_SIZE,
  335. MLXSW_SP_RESOURCE_KVD_LINEAR_SINGLE,
  336. MLXSW_SP_RESOURCE_KVD_LINEAR,
  337. &size_params);
  338. if (err)
  339. return err;
  340. devlink_resource_size_params_init(&size_params, 0, kvdl_max_size,
  341. MLXSW_SP1_KVDL_CHUNKS_ALLOC_SIZE,
  342. DEVLINK_RESOURCE_UNIT_ENTRY);
  343. err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR_CHUNKS,
  344. MLXSW_SP1_KVDL_CHUNKS_SIZE,
  345. MLXSW_SP_RESOURCE_KVD_LINEAR_CHUNKS,
  346. MLXSW_SP_RESOURCE_KVD_LINEAR,
  347. &size_params);
  348. if (err)
  349. return err;
  350. devlink_resource_size_params_init(&size_params, 0, kvdl_max_size,
  351. MLXSW_SP1_KVDL_LARGE_CHUNKS_ALLOC_SIZE,
  352. DEVLINK_RESOURCE_UNIT_ENTRY);
  353. err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR_LARGE_CHUNKS,
  354. MLXSW_SP1_KVDL_LARGE_CHUNKS_SIZE,
  355. MLXSW_SP_RESOURCE_KVD_LINEAR_LARGE_CHUNKS,
  356. MLXSW_SP_RESOURCE_KVD_LINEAR,
  357. &size_params);
  358. return err;
  359. }