blob: 119f8ebab5f6c2cd025e39ee1fdc63d0351f5a09 [file] [log] [blame]
Will Deaconfdb1d7b2014-11-14 17:16:49 +00001/*
2 * Generic page table allocator for IOMMUs.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Copyright (C) 2014 ARM Limited
17 *
18 * Author: Will Deacon <will.deacon@arm.com>
19 */
20
Mitchel Humpherysf01d6e32015-07-15 18:25:07 -070021#define pr_fmt(fmt) "io-pgtable: " fmt
22
Will Deaconfdb1d7b2014-11-14 17:16:49 +000023#include <linux/bug.h>
24#include <linux/kernel.h>
25#include <linux/types.h>
Mitchel Humpherysf01d6e32015-07-15 18:25:07 -070026#include <linux/iommu.h>
27#include <linux/debugfs.h>
28#include <linux/atomic.h>
29#include <linux/module.h>
Will Deaconfdb1d7b2014-11-14 17:16:49 +000030
31#include "io-pgtable.h"
32
33static const struct io_pgtable_init_fns *
Cosmin-Gabriel Samoila54c6d242016-03-13 02:17:27 +020034io_pgtable_init_table[IO_PGTABLE_NUM_FMTS] = {
Will Deacone1d3c0f2014-11-14 17:18:23 +000035#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE
36 [ARM_32_LPAE_S1] = &io_pgtable_arm_32_lpae_s1_init_fns,
37 [ARM_32_LPAE_S2] = &io_pgtable_arm_32_lpae_s2_init_fns,
38 [ARM_64_LPAE_S1] = &io_pgtable_arm_64_lpae_s1_init_fns,
39 [ARM_64_LPAE_S2] = &io_pgtable_arm_64_lpae_s2_init_fns,
40#endif
Robin Murphye5fc9752016-01-26 17:13:13 +000041#ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S
42 [ARM_V7S] = &io_pgtable_arm_v7s_init_fns,
43#endif
Will Deaconfdb1d7b2014-11-14 17:16:49 +000044};
45
Mitchel Humpherysf01d6e32015-07-15 18:25:07 -070046static struct dentry *io_pgtable_top;
47
Will Deaconfdb1d7b2014-11-14 17:16:49 +000048struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
49 struct io_pgtable_cfg *cfg,
50 void *cookie)
51{
52 struct io_pgtable *iop;
53 const struct io_pgtable_init_fns *fns;
54
55 if (fmt >= IO_PGTABLE_NUM_FMTS)
56 return NULL;
57
58 fns = io_pgtable_init_table[fmt];
59 if (!fns)
60 return NULL;
61
62 iop = fns->alloc(cfg, cookie);
63 if (!iop)
64 return NULL;
65
66 iop->fmt = fmt;
67 iop->cookie = cookie;
68 iop->cfg = *cfg;
69
70 return &iop->ops;
71}
72
73/*
74 * It is the IOMMU driver's responsibility to ensure that the page table
75 * is no longer accessible to the walker by this point.
76 */
77void free_io_pgtable_ops(struct io_pgtable_ops *ops)
78{
79 struct io_pgtable *iop;
80
81 if (!ops)
82 return;
83
84 iop = container_of(ops, struct io_pgtable, ops);
Robin Murphy507e4c92016-01-26 17:13:14 +000085 io_pgtable_tlb_flush_all(iop);
Will Deaconfdb1d7b2014-11-14 17:16:49 +000086 io_pgtable_init_table[iop->fmt]->free(iop);
87}
Mitchel Humpherysf01d6e32015-07-15 18:25:07 -070088
89static atomic_t pages_allocated;
90
91void *io_pgtable_alloc_pages_exact(size_t size, gfp_t gfp_mask)
92{
93 void *ret = alloc_pages_exact(size, gfp_mask);
94
95 if (likely(ret))
96 atomic_add(1 << get_order(size), &pages_allocated);
97 return ret;
98}
99
100void io_pgtable_free_pages_exact(void *virt, size_t size)
101{
102 free_pages_exact(virt, size);
103 atomic_sub(1 << get_order(size), &pages_allocated);
104}
105
106static int io_pgtable_init(void)
107{
108 io_pgtable_top = debugfs_create_dir("io-pgtable", iommu_debugfs_top);
109
110 if (!io_pgtable_top)
111 return -ENODEV;
112
113 if (!debugfs_create_atomic_t("pages", 0600,
114 io_pgtable_top, &pages_allocated)) {
115 debugfs_remove_recursive(io_pgtable_top);
116 return -ENODEV;
117 }
118
119 return 0;
120}
121
122static void io_pgtable_exit(void)
123{
124 debugfs_remove_recursive(io_pgtable_top);
125}
126
127module_init(io_pgtable_init);
128module_exit(io_pgtable_exit);