pagealloc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (c) 2013, Mellanox Technologies inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <asm-generic/kmap_types.h>
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/mlx5/driver.h>
  36. #include <linux/mlx5/cmd.h>
  37. #include "mlx5_core.h"
  38. enum {
  39. MLX5_PAGES_CANT_GIVE = 0,
  40. MLX5_PAGES_GIVE = 1,
  41. MLX5_PAGES_TAKE = 2
  42. };
  43. enum {
  44. MLX5_BOOT_PAGES = 1,
  45. MLX5_INIT_PAGES = 2,
  46. MLX5_POST_INIT_PAGES = 3
  47. };
  48. struct mlx5_pages_req {
  49. struct mlx5_core_dev *dev;
  50. u16 func_id;
  51. s32 npages;
  52. struct work_struct work;
  53. };
  54. struct fw_page {
  55. struct rb_node rb_node;
  56. u64 addr;
  57. struct page *page;
  58. u16 func_id;
  59. unsigned long bitmask;
  60. struct list_head list;
  61. unsigned free_count;
  62. };
  63. struct mlx5_query_pages_inbox {
  64. struct mlx5_inbox_hdr hdr;
  65. u8 rsvd[8];
  66. };
  67. struct mlx5_query_pages_outbox {
  68. struct mlx5_outbox_hdr hdr;
  69. __be16 rsvd;
  70. __be16 func_id;
  71. __be32 num_pages;
  72. };
  73. struct mlx5_manage_pages_inbox {
  74. struct mlx5_inbox_hdr hdr;
  75. __be16 rsvd;
  76. __be16 func_id;
  77. __be32 num_entries;
  78. __be64 pas[0];
  79. };
  80. struct mlx5_manage_pages_outbox {
  81. struct mlx5_outbox_hdr hdr;
  82. __be32 num_entries;
  83. u8 rsvd[4];
  84. __be64 pas[0];
  85. };
  86. enum {
  87. MAX_RECLAIM_TIME_MSECS = 5000,
  88. };
  89. enum {
  90. MLX5_MAX_RECLAIM_TIME_MILI = 5000,
  91. MLX5_NUM_4K_IN_PAGE = PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE,
  92. };
  93. static int insert_page(struct mlx5_core_dev *dev, u64 addr, struct page *page, u16 func_id)
  94. {
  95. struct rb_root *root = &dev->priv.page_root;
  96. struct rb_node **new = &root->rb_node;
  97. struct rb_node *parent = NULL;
  98. struct fw_page *nfp;
  99. struct fw_page *tfp;
  100. int i;
  101. while (*new) {
  102. parent = *new;
  103. tfp = rb_entry(parent, struct fw_page, rb_node);
  104. if (tfp->addr < addr)
  105. new = &parent->rb_left;
  106. else if (tfp->addr > addr)
  107. new = &parent->rb_right;
  108. else
  109. return -EEXIST;
  110. }
  111. nfp = kzalloc(sizeof(*nfp), GFP_KERNEL);
  112. if (!nfp)
  113. return -ENOMEM;
  114. nfp->addr = addr;
  115. nfp->page = page;
  116. nfp->func_id = func_id;
  117. nfp->free_count = MLX5_NUM_4K_IN_PAGE;
  118. for (i = 0; i < MLX5_NUM_4K_IN_PAGE; i++)
  119. set_bit(i, &nfp->bitmask);
  120. rb_link_node(&nfp->rb_node, parent, new);
  121. rb_insert_color(&nfp->rb_node, root);
  122. list_add(&nfp->list, &dev->priv.free_list);
  123. return 0;
  124. }
  125. static struct fw_page *find_fw_page(struct mlx5_core_dev *dev, u64 addr)
  126. {
  127. struct rb_root *root = &dev->priv.page_root;
  128. struct rb_node *tmp = root->rb_node;
  129. struct fw_page *result = NULL;
  130. struct fw_page *tfp;
  131. while (tmp) {
  132. tfp = rb_entry(tmp, struct fw_page, rb_node);
  133. if (tfp->addr < addr) {
  134. tmp = tmp->rb_left;
  135. } else if (tfp->addr > addr) {
  136. tmp = tmp->rb_right;
  137. } else {
  138. result = tfp;
  139. break;
  140. }
  141. }
  142. return result;
  143. }
  144. static int mlx5_cmd_query_pages(struct mlx5_core_dev *dev, u16 *func_id,
  145. s32 *npages, int boot)
  146. {
  147. struct mlx5_query_pages_inbox in;
  148. struct mlx5_query_pages_outbox out;
  149. int err;
  150. memset(&in, 0, sizeof(in));
  151. memset(&out, 0, sizeof(out));
  152. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_QUERY_PAGES);
  153. in.hdr.opmod = boot ? cpu_to_be16(MLX5_BOOT_PAGES) : cpu_to_be16(MLX5_INIT_PAGES);
  154. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  155. if (err)
  156. return err;
  157. if (out.hdr.status)
  158. return mlx5_cmd_status_to_err(&out.hdr);
  159. *npages = be32_to_cpu(out.num_pages);
  160. *func_id = be16_to_cpu(out.func_id);
  161. return err;
  162. }
  163. static int alloc_4k(struct mlx5_core_dev *dev, u64 *addr)
  164. {
  165. struct fw_page *fp;
  166. unsigned n;
  167. if (list_empty(&dev->priv.free_list))
  168. return -ENOMEM;
  169. fp = list_entry(dev->priv.free_list.next, struct fw_page, list);
  170. n = find_first_bit(&fp->bitmask, 8 * sizeof(fp->bitmask));
  171. if (n >= MLX5_NUM_4K_IN_PAGE) {
  172. mlx5_core_warn(dev, "alloc 4k bug\n");
  173. return -ENOENT;
  174. }
  175. clear_bit(n, &fp->bitmask);
  176. fp->free_count--;
  177. if (!fp->free_count)
  178. list_del(&fp->list);
  179. *addr = fp->addr + n * MLX5_ADAPTER_PAGE_SIZE;
  180. return 0;
  181. }
  182. #define MLX5_U64_4K_PAGE_MASK ((~(u64)0U) << PAGE_SHIFT)
  183. static void free_4k(struct mlx5_core_dev *dev, u64 addr)
  184. {
  185. struct fw_page *fwp;
  186. int n;
  187. fwp = find_fw_page(dev, addr & MLX5_U64_4K_PAGE_MASK);
  188. if (!fwp) {
  189. mlx5_core_warn(dev, "page not found\n");
  190. return;
  191. }
  192. n = (addr & ~MLX5_U64_4K_PAGE_MASK) >> MLX5_ADAPTER_PAGE_SHIFT;
  193. fwp->free_count++;
  194. set_bit(n, &fwp->bitmask);
  195. if (fwp->free_count == MLX5_NUM_4K_IN_PAGE) {
  196. rb_erase(&fwp->rb_node, &dev->priv.page_root);
  197. if (fwp->free_count != 1)
  198. list_del(&fwp->list);
  199. dma_unmap_page(&dev->pdev->dev, addr & MLX5_U64_4K_PAGE_MASK,
  200. PAGE_SIZE, DMA_BIDIRECTIONAL);
  201. __free_page(fwp->page);
  202. kfree(fwp);
  203. } else if (fwp->free_count == 1) {
  204. list_add(&fwp->list, &dev->priv.free_list);
  205. }
  206. }
  207. static int alloc_system_page(struct mlx5_core_dev *dev, u16 func_id)
  208. {
  209. struct page *page;
  210. u64 addr;
  211. int err;
  212. page = alloc_page(GFP_HIGHUSER);
  213. if (!page) {
  214. mlx5_core_warn(dev, "failed to allocate page\n");
  215. return -ENOMEM;
  216. }
  217. addr = dma_map_page(&dev->pdev->dev, page, 0,
  218. PAGE_SIZE, DMA_BIDIRECTIONAL);
  219. if (dma_mapping_error(&dev->pdev->dev, addr)) {
  220. mlx5_core_warn(dev, "failed dma mapping page\n");
  221. err = -ENOMEM;
  222. goto out_alloc;
  223. }
  224. err = insert_page(dev, addr, page, func_id);
  225. if (err) {
  226. mlx5_core_err(dev, "failed to track allocated page\n");
  227. goto out_mapping;
  228. }
  229. return 0;
  230. out_mapping:
  231. dma_unmap_page(&dev->pdev->dev, addr, PAGE_SIZE, DMA_BIDIRECTIONAL);
  232. out_alloc:
  233. __free_page(page);
  234. return err;
  235. }
  236. static int give_pages(struct mlx5_core_dev *dev, u16 func_id, int npages,
  237. int notify_fail)
  238. {
  239. struct mlx5_manage_pages_inbox *in;
  240. struct mlx5_manage_pages_outbox out;
  241. struct mlx5_manage_pages_inbox *nin;
  242. int inlen;
  243. u64 addr;
  244. int err;
  245. int i;
  246. inlen = sizeof(*in) + npages * sizeof(in->pas[0]);
  247. in = mlx5_vzalloc(inlen);
  248. if (!in) {
  249. mlx5_core_warn(dev, "vzalloc failed %d\n", inlen);
  250. return -ENOMEM;
  251. }
  252. memset(&out, 0, sizeof(out));
  253. for (i = 0; i < npages; i++) {
  254. retry:
  255. err = alloc_4k(dev, &addr);
  256. if (err) {
  257. if (err == -ENOMEM)
  258. err = alloc_system_page(dev, func_id);
  259. if (err)
  260. goto out_4k;
  261. goto retry;
  262. }
  263. in->pas[i] = cpu_to_be64(addr);
  264. }
  265. in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
  266. in->hdr.opmod = cpu_to_be16(MLX5_PAGES_GIVE);
  267. in->func_id = cpu_to_be16(func_id);
  268. in->num_entries = cpu_to_be32(npages);
  269. err = mlx5_cmd_exec(dev, in, inlen, &out, sizeof(out));
  270. if (err) {
  271. mlx5_core_warn(dev, "func_id 0x%x, npages %d, err %d\n",
  272. func_id, npages, err);
  273. goto out_alloc;
  274. }
  275. dev->priv.fw_pages += npages;
  276. if (out.hdr.status) {
  277. err = mlx5_cmd_status_to_err(&out.hdr);
  278. if (err) {
  279. mlx5_core_warn(dev, "func_id 0x%x, npages %d, status %d\n",
  280. func_id, npages, out.hdr.status);
  281. goto out_alloc;
  282. }
  283. }
  284. mlx5_core_dbg(dev, "err %d\n", err);
  285. goto out_free;
  286. out_alloc:
  287. if (notify_fail) {
  288. nin = kzalloc(sizeof(*nin), GFP_KERNEL);
  289. if (!nin) {
  290. mlx5_core_warn(dev, "allocation failed\n");
  291. goto out_4k;
  292. }
  293. memset(&out, 0, sizeof(out));
  294. nin->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
  295. nin->hdr.opmod = cpu_to_be16(MLX5_PAGES_CANT_GIVE);
  296. if (mlx5_cmd_exec(dev, nin, sizeof(*nin), &out, sizeof(out)))
  297. mlx5_core_warn(dev, "page notify failed\n");
  298. kfree(nin);
  299. }
  300. out_4k:
  301. for (i--; i >= 0; i--)
  302. free_4k(dev, be64_to_cpu(in->pas[i]));
  303. out_free:
  304. mlx5_vfree(in);
  305. return err;
  306. }
  307. static int reclaim_pages(struct mlx5_core_dev *dev, u32 func_id, int npages,
  308. int *nclaimed)
  309. {
  310. struct mlx5_manage_pages_inbox in;
  311. struct mlx5_manage_pages_outbox *out;
  312. int num_claimed;
  313. int outlen;
  314. u64 addr;
  315. int err;
  316. int i;
  317. if (nclaimed)
  318. *nclaimed = 0;
  319. memset(&in, 0, sizeof(in));
  320. outlen = sizeof(*out) + npages * sizeof(out->pas[0]);
  321. out = mlx5_vzalloc(outlen);
  322. if (!out)
  323. return -ENOMEM;
  324. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MANAGE_PAGES);
  325. in.hdr.opmod = cpu_to_be16(MLX5_PAGES_TAKE);
  326. in.func_id = cpu_to_be16(func_id);
  327. in.num_entries = cpu_to_be32(npages);
  328. mlx5_core_dbg(dev, "npages %d, outlen %d\n", npages, outlen);
  329. err = mlx5_cmd_exec(dev, &in, sizeof(in), out, outlen);
  330. if (err) {
  331. mlx5_core_err(dev, "failed reclaiming pages\n");
  332. goto out_free;
  333. }
  334. dev->priv.fw_pages -= npages;
  335. if (out->hdr.status) {
  336. err = mlx5_cmd_status_to_err(&out->hdr);
  337. goto out_free;
  338. }
  339. num_claimed = be32_to_cpu(out->num_entries);
  340. if (nclaimed)
  341. *nclaimed = num_claimed;
  342. for (i = 0; i < num_claimed; i++) {
  343. addr = be64_to_cpu(out->pas[i]);
  344. free_4k(dev, addr);
  345. }
  346. out_free:
  347. mlx5_vfree(out);
  348. return err;
  349. }
  350. static void pages_work_handler(struct work_struct *work)
  351. {
  352. struct mlx5_pages_req *req = container_of(work, struct mlx5_pages_req, work);
  353. struct mlx5_core_dev *dev = req->dev;
  354. int err = 0;
  355. if (req->npages < 0)
  356. err = reclaim_pages(dev, req->func_id, -1 * req->npages, NULL);
  357. else if (req->npages > 0)
  358. err = give_pages(dev, req->func_id, req->npages, 1);
  359. if (err)
  360. mlx5_core_warn(dev, "%s fail %d\n",
  361. req->npages < 0 ? "reclaim" : "give", err);
  362. kfree(req);
  363. }
  364. void mlx5_core_req_pages_handler(struct mlx5_core_dev *dev, u16 func_id,
  365. s32 npages)
  366. {
  367. struct mlx5_pages_req *req;
  368. req = kzalloc(sizeof(*req), GFP_ATOMIC);
  369. if (!req) {
  370. mlx5_core_warn(dev, "failed to allocate pages request\n");
  371. return;
  372. }
  373. req->dev = dev;
  374. req->func_id = func_id;
  375. req->npages = npages;
  376. INIT_WORK(&req->work, pages_work_handler);
  377. queue_work(dev->priv.pg_wq, &req->work);
  378. }
  379. int mlx5_satisfy_startup_pages(struct mlx5_core_dev *dev, int boot)
  380. {
  381. u16 uninitialized_var(func_id);
  382. s32 uninitialized_var(npages);
  383. int err;
  384. err = mlx5_cmd_query_pages(dev, &func_id, &npages, boot);
  385. if (err)
  386. return err;
  387. mlx5_core_dbg(dev, "requested %d %s pages for func_id 0x%x\n",
  388. npages, boot ? "boot" : "init", func_id);
  389. return give_pages(dev, func_id, npages, 0);
  390. }
  391. enum {
  392. MLX5_BLKS_FOR_RECLAIM_PAGES = 12
  393. };
  394. static int optimal_reclaimed_pages(void)
  395. {
  396. struct mlx5_cmd_prot_block *block;
  397. struct mlx5_cmd_layout *lay;
  398. int ret;
  399. ret = (sizeof(lay->out) + MLX5_BLKS_FOR_RECLAIM_PAGES * sizeof(block->data) -
  400. sizeof(struct mlx5_manage_pages_outbox)) /
  401. FIELD_SIZEOF(struct mlx5_manage_pages_outbox, pas[0]);
  402. return ret;
  403. }
  404. int mlx5_reclaim_startup_pages(struct mlx5_core_dev *dev)
  405. {
  406. unsigned long end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
  407. struct fw_page *fwp;
  408. struct rb_node *p;
  409. int nclaimed = 0;
  410. int err;
  411. do {
  412. p = rb_first(&dev->priv.page_root);
  413. if (p) {
  414. fwp = rb_entry(p, struct fw_page, rb_node);
  415. err = reclaim_pages(dev, fwp->func_id,
  416. optimal_reclaimed_pages(),
  417. &nclaimed);
  418. if (err) {
  419. mlx5_core_warn(dev, "failed reclaiming pages (%d)\n",
  420. err);
  421. return err;
  422. }
  423. if (nclaimed)
  424. end = jiffies + msecs_to_jiffies(MAX_RECLAIM_TIME_MSECS);
  425. }
  426. if (time_after(jiffies, end)) {
  427. mlx5_core_warn(dev, "FW did not return all pages. giving up...\n");
  428. break;
  429. }
  430. } while (p);
  431. return 0;
  432. }
  433. void mlx5_pagealloc_init(struct mlx5_core_dev *dev)
  434. {
  435. dev->priv.page_root = RB_ROOT;
  436. INIT_LIST_HEAD(&dev->priv.free_list);
  437. }
  438. void mlx5_pagealloc_cleanup(struct mlx5_core_dev *dev)
  439. {
  440. /* nothing */
  441. }
  442. int mlx5_pagealloc_start(struct mlx5_core_dev *dev)
  443. {
  444. dev->priv.pg_wq = create_singlethread_workqueue("mlx5_page_allocator");
  445. if (!dev->priv.pg_wq)
  446. return -ENOMEM;
  447. return 0;
  448. }
  449. void mlx5_pagealloc_stop(struct mlx5_core_dev *dev)
  450. {
  451. destroy_workqueue(dev->priv.pg_wq);
  452. }