blob: 0844c1d05ac619b5d3c0c58263088d0f09fbdcc1 [file] [log] [blame]
Leon Romanovsky6bf9d8f2020-07-19 10:25:21 +03001/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
Shachar Raindel8ada2c12014-12-11 17:04:17 +02002/*
3 * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
Shachar Raindel8ada2c12014-12-11 17:04:17 +02004 */
5
6#ifndef IB_UMEM_ODP_H
7#define IB_UMEM_ODP_H
8
9#include <rdma/ib_umem.h>
Haggai Eran882214e2014-12-11 17:04:18 +020010#include <rdma/ib_verbs.h>
Haggai Eran882214e2014-12-11 17:04:18 +020011
Shachar Raindel8ada2c12014-12-11 17:04:17 +020012struct ib_umem_odp {
Jason Gunthorpe41b4deea2018-09-16 20:48:05 +030013 struct ib_umem umem;
Jason Gunthorpef25a5462019-11-12 16:22:22 -040014 struct mmu_interval_notifier notifier;
15 struct pid *tgid;
Jason Gunthorpec9990ab2018-09-16 20:48:07 +030016
Yishai Hadas36f30e42020-09-30 19:38:25 +030017 /* An array of the pfns included in the on-demand paging umem. */
18 unsigned long *pfn_list;
19
Shachar Raindel8ada2c12014-12-11 17:04:17 +020020 /*
Yishai Hadas36f30e42020-09-30 19:38:25 +030021 * An array with DMA addresses mapped for pfns in pfn_list.
22 * The lower two bits designate access permissions.
23 * See ODP_READ_ALLOWED_BIT and ODP_WRITE_ALLOWED_BIT.
Shachar Raindel8ada2c12014-12-11 17:04:17 +020024 */
25 dma_addr_t *dma_list;
26 /*
27 * The umem_mutex protects the page_list and dma_list fields of an ODP
Haggai Eran882214e2014-12-11 17:04:18 +020028 * umem, allowing only a single thread to map/unmap pages. The mutex
29 * also protects access to the mmu notifier counters.
Shachar Raindel8ada2c12014-12-11 17:04:17 +020030 */
31 struct mutex umem_mutex;
32 void *private; /* for the HW driver to use. */
Haggai Eran882214e2014-12-11 17:04:18 +020033
Shiraz Saleemd10bcf92019-04-02 14:52:52 -050034 int npages;
Haggai Eran882214e2014-12-11 17:04:18 +020035
Jason Gunthorpefd7dbf02019-08-19 14:17:01 +030036 /*
37 * An implicit odp umem cannot be DMA mapped, has 0 length, and serves
38 * only as an anchor for the driver to hold onto the per_mm. FIXME:
39 * This should be removed and drivers should work with the per_mm
40 * directly.
41 */
42 bool is_implicit_odp;
43
Jason Gunthorped2183c62019-05-20 09:05:25 +030044 unsigned int page_shift;
Shachar Raindel8ada2c12014-12-11 17:04:17 +020045};
46
Jason Gunthorpeb5231b02018-09-16 20:48:04 +030047static inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem)
48{
Jason Gunthorpe41b4deea2018-09-16 20:48:05 +030049 return container_of(umem, struct ib_umem_odp, umem);
Jason Gunthorpeb5231b02018-09-16 20:48:04 +030050}
51
Jason Gunthorped2183c62019-05-20 09:05:25 +030052/* Returns the first page of an ODP umem. */
53static inline unsigned long ib_umem_start(struct ib_umem_odp *umem_odp)
54{
Jason Gunthorpef25a5462019-11-12 16:22:22 -040055 return umem_odp->notifier.interval_tree.start;
Jason Gunthorped2183c62019-05-20 09:05:25 +030056}
57
58/* Returns the address of the page after the last one of an ODP umem. */
59static inline unsigned long ib_umem_end(struct ib_umem_odp *umem_odp)
60{
Jason Gunthorpef25a5462019-11-12 16:22:22 -040061 return umem_odp->notifier.interval_tree.last + 1;
Jason Gunthorped2183c62019-05-20 09:05:25 +030062}
63
64static inline size_t ib_umem_odp_num_pages(struct ib_umem_odp *umem_odp)
65{
66 return (ib_umem_end(umem_odp) - ib_umem_start(umem_odp)) >>
67 umem_odp->page_shift;
68}
69
Leon Romanovsky13859d5d2019-01-08 16:07:26 +020070/*
71 * The lower 2 bits of the DMA address signal the R/W permissions for
72 * the entry. To upgrade the permissions, provide the appropriate
73 * bitmask to the map_dma_pages function.
74 *
75 * Be aware that upgrading a mapped address might result in change of
76 * the DMA address for the page.
77 */
78#define ODP_READ_ALLOWED_BIT (1<<0ULL)
79#define ODP_WRITE_ALLOWED_BIT (1<<1ULL)
80
81#define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT))
82
Shachar Raindel8ada2c12014-12-11 17:04:17 +020083#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
84
Jason Gunthorpef25a5462019-11-12 16:22:22 -040085struct ib_umem_odp *
Moni Shouac320e522020-01-15 14:43:31 +020086ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size,
Jason Gunthorpef25a5462019-11-12 16:22:22 -040087 int access, const struct mmu_interval_notifier_ops *ops);
Moni Shouac320e522020-01-15 14:43:31 +020088struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device,
Jason Gunthorpef20bef62019-08-19 14:17:03 +030089 int access);
Jason Gunthorpef25a5462019-11-12 16:22:22 -040090struct ib_umem_odp *
91ib_umem_odp_alloc_child(struct ib_umem_odp *root_umem, unsigned long addr,
92 size_t size,
93 const struct mmu_interval_notifier_ops *ops);
Jason Gunthorpeb5231b02018-09-16 20:48:04 +030094void ib_umem_odp_release(struct ib_umem_odp *umem_odp);
Shachar Raindel8ada2c12014-12-11 17:04:17 +020095
Yishai Hadas36f30e42020-09-30 19:38:25 +030096int ib_umem_odp_map_dma_and_lock(struct ib_umem_odp *umem_odp, u64 start_offset,
Yishai Hadas8bfafde2020-09-30 19:38:26 +030097 u64 bcnt, u64 access_mask, bool fault);
Shachar Raindel8ada2c12014-12-11 17:04:17 +020098
Jason Gunthorpeb5231b02018-09-16 20:48:04 +030099void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset,
Shachar Raindel8ada2c12014-12-11 17:04:17 +0200100 u64 bound);
101
Shachar Raindel8ada2c12014-12-11 17:04:17 +0200102#else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
103
Jason Gunthorpef25a5462019-11-12 16:22:22 -0400104static inline struct ib_umem_odp *
Moni Shouac320e522020-01-15 14:43:31 +0200105ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size,
Jason Gunthorpef25a5462019-11-12 16:22:22 -0400106 int access, const struct mmu_interval_notifier_ops *ops)
Shachar Raindel8ada2c12014-12-11 17:04:17 +0200107{
Jason Gunthorpe261dc532019-08-19 14:17:04 +0300108 return ERR_PTR(-EINVAL);
Shachar Raindel8ada2c12014-12-11 17:04:17 +0200109}
110
Jason Gunthorpeb5231b02018-09-16 20:48:04 +0300111static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {}
Shachar Raindel8ada2c12014-12-11 17:04:17 +0200112
113#endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
114
115#endif /* IB_UMEM_ODP_H */