]>
Commit | Line | Data |
---|---|---|
b6c02715 JR |
1 | /* |
2 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. | |
3 | * Author: Joerg Roedel <joerg.roedel@amd.com> | |
4 | * Leo Duran <leo.duran@amd.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License version 2 as published | |
8 | * by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | ||
20 | #include <linux/pci.h> | |
21 | #include <linux/gfp.h> | |
22 | #include <linux/bitops.h> | |
7f26508b | 23 | #include <linux/debugfs.h> |
b6c02715 JR |
24 | #include <linux/scatterlist.h> |
25 | #include <linux/iommu-helper.h> | |
c156e347 JR |
26 | #ifdef CONFIG_IOMMU_API |
27 | #include <linux/iommu.h> | |
28 | #endif | |
b6c02715 | 29 | #include <asm/proto.h> |
46a7fa27 | 30 | #include <asm/iommu.h> |
1d9b16d1 | 31 | #include <asm/gart.h> |
b6c02715 | 32 | #include <asm/amd_iommu_types.h> |
c6da992e | 33 | #include <asm/amd_iommu.h> |
b6c02715 JR |
34 | |
35 | #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) | |
36 | ||
136f78a1 JR |
37 | #define EXIT_LOOP_COUNT 10000000 |
38 | ||
b6c02715 JR |
39 | static DEFINE_RWLOCK(amd_iommu_devtable_lock); |
40 | ||
bd60b735 JR |
41 | /* A list of preallocated protection domains */ |
42 | static LIST_HEAD(iommu_pd_list); | |
43 | static DEFINE_SPINLOCK(iommu_pd_list_lock); | |
44 | ||
26961efe JR |
45 | #ifdef CONFIG_IOMMU_API |
46 | static struct iommu_ops amd_iommu_ops; | |
47 | #endif | |
48 | ||
431b2a20 JR |
49 | /* |
50 | * general struct to manage commands send to an IOMMU | |
51 | */ | |
d6449536 | 52 | struct iommu_cmd { |
b6c02715 JR |
53 | u32 data[4]; |
54 | }; | |
55 | ||
bd0e5211 JR |
56 | static int dma_ops_unity_map(struct dma_ops_domain *dma_dom, |
57 | struct unity_map_entry *e); | |
e275a2a0 JR |
58 | static struct dma_ops_domain *find_protection_domain(u16 devid); |
59 | ||
bd0e5211 | 60 | |
7f26508b JR |
61 | #ifdef CONFIG_AMD_IOMMU_STATS |
62 | ||
63 | /* | |
64 | * Initialization code for statistics collection | |
65 | */ | |
66 | ||
da49f6df | 67 | DECLARE_STATS_COUNTER(compl_wait); |
0f2a86f2 | 68 | DECLARE_STATS_COUNTER(cnt_map_single); |
146a6917 | 69 | DECLARE_STATS_COUNTER(cnt_unmap_single); |
d03f067a | 70 | DECLARE_STATS_COUNTER(cnt_map_sg); |
55877a6b | 71 | DECLARE_STATS_COUNTER(cnt_unmap_sg); |
c8f0fb36 | 72 | DECLARE_STATS_COUNTER(cnt_alloc_coherent); |
5d31ee7e | 73 | DECLARE_STATS_COUNTER(cnt_free_coherent); |
da49f6df | 74 | |
7f26508b JR |
75 | static struct dentry *stats_dir; |
76 | static struct dentry *de_isolate; | |
77 | static struct dentry *de_fflush; | |
78 | ||
79 | static void amd_iommu_stats_add(struct __iommu_counter *cnt) | |
80 | { | |
81 | if (stats_dir == NULL) | |
82 | return; | |
83 | ||
84 | cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir, | |
85 | &cnt->value); | |
86 | } | |
87 | ||
88 | static void amd_iommu_stats_init(void) | |
89 | { | |
90 | stats_dir = debugfs_create_dir("amd-iommu", NULL); | |
91 | if (stats_dir == NULL) | |
92 | return; | |
93 | ||
94 | de_isolate = debugfs_create_bool("isolation", 0444, stats_dir, | |
95 | (u32 *)&amd_iommu_isolate); | |
96 | ||
97 | de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir, | |
98 | (u32 *)&amd_iommu_unmap_flush); | |
da49f6df JR |
99 | |
100 | amd_iommu_stats_add(&compl_wait); | |
0f2a86f2 | 101 | amd_iommu_stats_add(&cnt_map_single); |
146a6917 | 102 | amd_iommu_stats_add(&cnt_unmap_single); |
d03f067a | 103 | amd_iommu_stats_add(&cnt_map_sg); |
55877a6b | 104 | amd_iommu_stats_add(&cnt_unmap_sg); |
c8f0fb36 | 105 | amd_iommu_stats_add(&cnt_alloc_coherent); |
5d31ee7e | 106 | amd_iommu_stats_add(&cnt_free_coherent); |
7f26508b JR |
107 | } |
108 | ||
109 | #endif | |
110 | ||
431b2a20 | 111 | /* returns !0 if the IOMMU is caching non-present entries in its TLB */ |
4da70b9e JR |
112 | static int iommu_has_npcache(struct amd_iommu *iommu) |
113 | { | |
ae9b9403 | 114 | return iommu->cap & (1UL << IOMMU_CAP_NPCACHE); |
4da70b9e JR |
115 | } |
116 | ||
a80dc3e0 JR |
117 | /**************************************************************************** |
118 | * | |
119 | * Interrupt handling functions | |
120 | * | |
121 | ****************************************************************************/ | |
122 | ||
90008ee4 JR |
123 | static void iommu_print_event(void *__evt) |
124 | { | |
125 | u32 *event = __evt; | |
126 | int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK; | |
127 | int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK; | |
128 | int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK; | |
129 | int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK; | |
130 | u64 address = (u64)(((u64)event[3]) << 32) | event[2]; | |
131 | ||
132 | printk(KERN_ERR "AMD IOMMU: Event logged ["); | |
133 | ||
134 | switch (type) { | |
135 | case EVENT_TYPE_ILL_DEV: | |
136 | printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x " | |
137 | "address=0x%016llx flags=0x%04x]\n", | |
138 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
139 | address, flags); | |
140 | break; | |
141 | case EVENT_TYPE_IO_FAULT: | |
142 | printk("IO_PAGE_FAULT device=%02x:%02x.%x " | |
143 | "domain=0x%04x address=0x%016llx flags=0x%04x]\n", | |
144 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
145 | domid, address, flags); | |
146 | break; | |
147 | case EVENT_TYPE_DEV_TAB_ERR: | |
148 | printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x " | |
149 | "address=0x%016llx flags=0x%04x]\n", | |
150 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
151 | address, flags); | |
152 | break; | |
153 | case EVENT_TYPE_PAGE_TAB_ERR: | |
154 | printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x " | |
155 | "domain=0x%04x address=0x%016llx flags=0x%04x]\n", | |
156 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
157 | domid, address, flags); | |
158 | break; | |
159 | case EVENT_TYPE_ILL_CMD: | |
160 | printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address); | |
161 | break; | |
162 | case EVENT_TYPE_CMD_HARD_ERR: | |
163 | printk("COMMAND_HARDWARE_ERROR address=0x%016llx " | |
164 | "flags=0x%04x]\n", address, flags); | |
165 | break; | |
166 | case EVENT_TYPE_IOTLB_INV_TO: | |
167 | printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x " | |
168 | "address=0x%016llx]\n", | |
169 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
170 | address); | |
171 | break; | |
172 | case EVENT_TYPE_INV_DEV_REQ: | |
173 | printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x " | |
174 | "address=0x%016llx flags=0x%04x]\n", | |
175 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), | |
176 | address, flags); | |
177 | break; | |
178 | default: | |
179 | printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type); | |
180 | } | |
181 | } | |
182 | ||
183 | static void iommu_poll_events(struct amd_iommu *iommu) | |
184 | { | |
185 | u32 head, tail; | |
186 | unsigned long flags; | |
187 | ||
188 | spin_lock_irqsave(&iommu->lock, flags); | |
189 | ||
190 | head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); | |
191 | tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); | |
192 | ||
193 | while (head != tail) { | |
194 | iommu_print_event(iommu->evt_buf + head); | |
195 | head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size; | |
196 | } | |
197 | ||
198 | writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); | |
199 | ||
200 | spin_unlock_irqrestore(&iommu->lock, flags); | |
201 | } | |
202 | ||
a80dc3e0 JR |
203 | irqreturn_t amd_iommu_int_handler(int irq, void *data) |
204 | { | |
90008ee4 JR |
205 | struct amd_iommu *iommu; |
206 | ||
207 | list_for_each_entry(iommu, &amd_iommu_list, list) | |
208 | iommu_poll_events(iommu); | |
209 | ||
210 | return IRQ_HANDLED; | |
a80dc3e0 JR |
211 | } |
212 | ||
431b2a20 JR |
213 | /**************************************************************************** |
214 | * | |
215 | * IOMMU command queuing functions | |
216 | * | |
217 | ****************************************************************************/ | |
218 | ||
219 | /* | |
220 | * Writes the command to the IOMMUs command buffer and informs the | |
221 | * hardware about the new command. Must be called with iommu->lock held. | |
222 | */ | |
d6449536 | 223 | static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) |
a19ae1ec JR |
224 | { |
225 | u32 tail, head; | |
226 | u8 *target; | |
227 | ||
228 | tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); | |
8a7c5ef3 | 229 | target = iommu->cmd_buf + tail; |
a19ae1ec JR |
230 | memcpy_toio(target, cmd, sizeof(*cmd)); |
231 | tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size; | |
232 | head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET); | |
233 | if (tail == head) | |
234 | return -ENOMEM; | |
235 | writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); | |
236 | ||
237 | return 0; | |
238 | } | |
239 | ||
431b2a20 JR |
240 | /* |
241 | * General queuing function for commands. Takes iommu->lock and calls | |
242 | * __iommu_queue_command(). | |
243 | */ | |
d6449536 | 244 | static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) |
a19ae1ec JR |
245 | { |
246 | unsigned long flags; | |
247 | int ret; | |
248 | ||
249 | spin_lock_irqsave(&iommu->lock, flags); | |
250 | ret = __iommu_queue_command(iommu, cmd); | |
09ee17eb | 251 | if (!ret) |
0cfd7aa9 | 252 | iommu->need_sync = true; |
a19ae1ec JR |
253 | spin_unlock_irqrestore(&iommu->lock, flags); |
254 | ||
255 | return ret; | |
256 | } | |
257 | ||
8d201968 JR |
258 | /* |
259 | * This function waits until an IOMMU has completed a completion | |
260 | * wait command | |
261 | */ | |
262 | static void __iommu_wait_for_completion(struct amd_iommu *iommu) | |
263 | { | |
264 | int ready = 0; | |
265 | unsigned status = 0; | |
266 | unsigned long i = 0; | |
267 | ||
da49f6df JR |
268 | INC_STATS_COUNTER(compl_wait); |
269 | ||
8d201968 JR |
270 | while (!ready && (i < EXIT_LOOP_COUNT)) { |
271 | ++i; | |
272 | /* wait for the bit to become one */ | |
273 | status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET); | |
274 | ready = status & MMIO_STATUS_COM_WAIT_INT_MASK; | |
275 | } | |
276 | ||
277 | /* set bit back to zero */ | |
278 | status &= ~MMIO_STATUS_COM_WAIT_INT_MASK; | |
279 | writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET); | |
280 | ||
281 | if (unlikely(i == EXIT_LOOP_COUNT)) | |
282 | panic("AMD IOMMU: Completion wait loop failed\n"); | |
283 | } | |
284 | ||
285 | /* | |
286 | * This function queues a completion wait command into the command | |
287 | * buffer of an IOMMU | |
288 | */ | |
289 | static int __iommu_completion_wait(struct amd_iommu *iommu) | |
290 | { | |
291 | struct iommu_cmd cmd; | |
292 | ||
293 | memset(&cmd, 0, sizeof(cmd)); | |
294 | cmd.data[0] = CMD_COMPL_WAIT_INT_MASK; | |
295 | CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT); | |
296 | ||
297 | return __iommu_queue_command(iommu, &cmd); | |
298 | } | |
299 | ||
431b2a20 JR |
300 | /* |
301 | * This function is called whenever we need to ensure that the IOMMU has | |
302 | * completed execution of all commands we sent. It sends a | |
303 | * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs | |
304 | * us about that by writing a value to a physical address we pass with | |
305 | * the command. | |
306 | */ | |
a19ae1ec JR |
307 | static int iommu_completion_wait(struct amd_iommu *iommu) |
308 | { | |
8d201968 JR |
309 | int ret = 0; |
310 | unsigned long flags; | |
a19ae1ec | 311 | |
7e4f88da JR |
312 | spin_lock_irqsave(&iommu->lock, flags); |
313 | ||
09ee17eb JR |
314 | if (!iommu->need_sync) |
315 | goto out; | |
316 | ||
8d201968 | 317 | ret = __iommu_completion_wait(iommu); |
09ee17eb | 318 | |
0cfd7aa9 | 319 | iommu->need_sync = false; |
a19ae1ec JR |
320 | |
321 | if (ret) | |
7e4f88da | 322 | goto out; |
a19ae1ec | 323 | |
8d201968 | 324 | __iommu_wait_for_completion(iommu); |
84df8175 | 325 | |
7e4f88da JR |
326 | out: |
327 | spin_unlock_irqrestore(&iommu->lock, flags); | |
a19ae1ec JR |
328 | |
329 | return 0; | |
330 | } | |
331 | ||
431b2a20 JR |
332 | /* |
333 | * Command send function for invalidating a device table entry | |
334 | */ | |
a19ae1ec JR |
335 | static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid) |
336 | { | |
d6449536 | 337 | struct iommu_cmd cmd; |
ee2fa743 | 338 | int ret; |
a19ae1ec JR |
339 | |
340 | BUG_ON(iommu == NULL); | |
341 | ||
342 | memset(&cmd, 0, sizeof(cmd)); | |
343 | CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY); | |
344 | cmd.data[0] = devid; | |
345 | ||
ee2fa743 JR |
346 | ret = iommu_queue_command(iommu, &cmd); |
347 | ||
ee2fa743 | 348 | return ret; |
a19ae1ec JR |
349 | } |
350 | ||
237b6f33 JR |
351 | static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, |
352 | u16 domid, int pde, int s) | |
353 | { | |
354 | memset(cmd, 0, sizeof(*cmd)); | |
355 | address &= PAGE_MASK; | |
356 | CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); | |
357 | cmd->data[1] |= domid; | |
358 | cmd->data[2] = lower_32_bits(address); | |
359 | cmd->data[3] = upper_32_bits(address); | |
360 | if (s) /* size bit - we flush more than one 4kb page */ | |
361 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; | |
362 | if (pde) /* PDE bit - we wan't flush everything not only the PTEs */ | |
363 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; | |
364 | } | |
365 | ||
431b2a20 JR |
366 | /* |
367 | * Generic command send function for invalidaing TLB entries | |
368 | */ | |
a19ae1ec JR |
369 | static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu, |
370 | u64 address, u16 domid, int pde, int s) | |
371 | { | |
d6449536 | 372 | struct iommu_cmd cmd; |
ee2fa743 | 373 | int ret; |
a19ae1ec | 374 | |
237b6f33 | 375 | __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s); |
a19ae1ec | 376 | |
ee2fa743 JR |
377 | ret = iommu_queue_command(iommu, &cmd); |
378 | ||
ee2fa743 | 379 | return ret; |
a19ae1ec JR |
380 | } |
381 | ||
431b2a20 JR |
382 | /* |
383 | * TLB invalidation function which is called from the mapping functions. | |
384 | * It invalidates a single PTE if the range to flush is within a single | |
385 | * page. Otherwise it flushes the whole TLB of the IOMMU. | |
386 | */ | |
a19ae1ec JR |
387 | static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid, |
388 | u64 address, size_t size) | |
389 | { | |
999ba417 | 390 | int s = 0; |
e3c449f5 | 391 | unsigned pages = iommu_num_pages(address, size, PAGE_SIZE); |
a19ae1ec JR |
392 | |
393 | address &= PAGE_MASK; | |
394 | ||
999ba417 JR |
395 | if (pages > 1) { |
396 | /* | |
397 | * If we have to flush more than one page, flush all | |
398 | * TLB entries for this domain | |
399 | */ | |
400 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; | |
401 | s = 1; | |
a19ae1ec JR |
402 | } |
403 | ||
999ba417 JR |
404 | iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s); |
405 | ||
a19ae1ec JR |
406 | return 0; |
407 | } | |
b6c02715 | 408 | |
1c655773 JR |
409 | /* Flush the whole IO/TLB for a given protection domain */ |
410 | static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid) | |
411 | { | |
412 | u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; | |
413 | ||
414 | iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1); | |
415 | } | |
416 | ||
43f49609 JR |
417 | #ifdef CONFIG_IOMMU_API |
418 | /* | |
419 | * This function is used to flush the IO/TLB for a given protection domain | |
420 | * on every IOMMU in the system | |
421 | */ | |
422 | static void iommu_flush_domain(u16 domid) | |
423 | { | |
424 | unsigned long flags; | |
425 | struct amd_iommu *iommu; | |
426 | struct iommu_cmd cmd; | |
427 | ||
428 | __iommu_build_inv_iommu_pages(&cmd, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, | |
429 | domid, 1, 1); | |
430 | ||
431 | list_for_each_entry(iommu, &amd_iommu_list, list) { | |
432 | spin_lock_irqsave(&iommu->lock, flags); | |
433 | __iommu_queue_command(iommu, &cmd); | |
434 | __iommu_completion_wait(iommu); | |
435 | __iommu_wait_for_completion(iommu); | |
436 | spin_unlock_irqrestore(&iommu->lock, flags); | |
437 | } | |
438 | } | |
439 | #endif | |
440 | ||
431b2a20 JR |
441 | /**************************************************************************** |
442 | * | |
443 | * The functions below are used the create the page table mappings for | |
444 | * unity mapped regions. | |
445 | * | |
446 | ****************************************************************************/ | |
447 | ||
448 | /* | |
449 | * Generic mapping functions. It maps a physical address into a DMA | |
450 | * address space. It allocates the page table pages if necessary. | |
451 | * In the future it can be extended to a generic mapping function | |
452 | * supporting all features of AMD IOMMU page tables like level skipping | |
453 | * and full 64 bit address spaces. | |
454 | */ | |
38e817fe JR |
455 | static int iommu_map_page(struct protection_domain *dom, |
456 | unsigned long bus_addr, | |
457 | unsigned long phys_addr, | |
458 | int prot) | |
bd0e5211 JR |
459 | { |
460 | u64 __pte, *pte, *page; | |
461 | ||
462 | bus_addr = PAGE_ALIGN(bus_addr); | |
bb9d4ff8 | 463 | phys_addr = PAGE_ALIGN(phys_addr); |
bd0e5211 JR |
464 | |
465 | /* only support 512GB address spaces for now */ | |
466 | if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK)) | |
467 | return -EINVAL; | |
468 | ||
469 | pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)]; | |
470 | ||
471 | if (!IOMMU_PTE_PRESENT(*pte)) { | |
472 | page = (u64 *)get_zeroed_page(GFP_KERNEL); | |
473 | if (!page) | |
474 | return -ENOMEM; | |
475 | *pte = IOMMU_L2_PDE(virt_to_phys(page)); | |
476 | } | |
477 | ||
478 | pte = IOMMU_PTE_PAGE(*pte); | |
479 | pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)]; | |
480 | ||
481 | if (!IOMMU_PTE_PRESENT(*pte)) { | |
482 | page = (u64 *)get_zeroed_page(GFP_KERNEL); | |
483 | if (!page) | |
484 | return -ENOMEM; | |
485 | *pte = IOMMU_L1_PDE(virt_to_phys(page)); | |
486 | } | |
487 | ||
488 | pte = IOMMU_PTE_PAGE(*pte); | |
489 | pte = &pte[IOMMU_PTE_L0_INDEX(bus_addr)]; | |
490 | ||
491 | if (IOMMU_PTE_PRESENT(*pte)) | |
492 | return -EBUSY; | |
493 | ||
494 | __pte = phys_addr | IOMMU_PTE_P; | |
495 | if (prot & IOMMU_PROT_IR) | |
496 | __pte |= IOMMU_PTE_IR; | |
497 | if (prot & IOMMU_PROT_IW) | |
498 | __pte |= IOMMU_PTE_IW; | |
499 | ||
500 | *pte = __pte; | |
501 | ||
502 | return 0; | |
503 | } | |
504 | ||
eb74ff6c JR |
505 | #ifdef CONFIG_IOMMU_API |
506 | static void iommu_unmap_page(struct protection_domain *dom, | |
507 | unsigned long bus_addr) | |
508 | { | |
509 | u64 *pte; | |
510 | ||
511 | pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)]; | |
512 | ||
513 | if (!IOMMU_PTE_PRESENT(*pte)) | |
514 | return; | |
515 | ||
516 | pte = IOMMU_PTE_PAGE(*pte); | |
517 | pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)]; | |
518 | ||
519 | if (!IOMMU_PTE_PRESENT(*pte)) | |
520 | return; | |
521 | ||
522 | pte = IOMMU_PTE_PAGE(*pte); | |
523 | pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)]; | |
524 | ||
525 | *pte = 0; | |
526 | } | |
527 | #endif | |
528 | ||
431b2a20 JR |
529 | /* |
530 | * This function checks if a specific unity mapping entry is needed for | |
531 | * this specific IOMMU. | |
532 | */ | |
bd0e5211 JR |
533 | static int iommu_for_unity_map(struct amd_iommu *iommu, |
534 | struct unity_map_entry *entry) | |
535 | { | |
536 | u16 bdf, i; | |
537 | ||
538 | for (i = entry->devid_start; i <= entry->devid_end; ++i) { | |
539 | bdf = amd_iommu_alias_table[i]; | |
540 | if (amd_iommu_rlookup_table[bdf] == iommu) | |
541 | return 1; | |
542 | } | |
543 | ||
544 | return 0; | |
545 | } | |
546 | ||
431b2a20 JR |
547 | /* |
548 | * Init the unity mappings for a specific IOMMU in the system | |
549 | * | |
550 | * Basically iterates over all unity mapping entries and applies them to | |
551 | * the default domain DMA of that IOMMU if necessary. | |
552 | */ | |
bd0e5211 JR |
553 | static int iommu_init_unity_mappings(struct amd_iommu *iommu) |
554 | { | |
555 | struct unity_map_entry *entry; | |
556 | int ret; | |
557 | ||
558 | list_for_each_entry(entry, &amd_iommu_unity_map, list) { | |
559 | if (!iommu_for_unity_map(iommu, entry)) | |
560 | continue; | |
561 | ret = dma_ops_unity_map(iommu->default_dom, entry); | |
562 | if (ret) | |
563 | return ret; | |
564 | } | |
565 | ||
566 | return 0; | |
567 | } | |
568 | ||
431b2a20 JR |
569 | /* |
570 | * This function actually applies the mapping to the page table of the | |
571 | * dma_ops domain. | |
572 | */ | |
bd0e5211 JR |
573 | static int dma_ops_unity_map(struct dma_ops_domain *dma_dom, |
574 | struct unity_map_entry *e) | |
575 | { | |
576 | u64 addr; | |
577 | int ret; | |
578 | ||
579 | for (addr = e->address_start; addr < e->address_end; | |
580 | addr += PAGE_SIZE) { | |
38e817fe | 581 | ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot); |
bd0e5211 JR |
582 | if (ret) |
583 | return ret; | |
584 | /* | |
585 | * if unity mapping is in aperture range mark the page | |
586 | * as allocated in the aperture | |
587 | */ | |
588 | if (addr < dma_dom->aperture_size) | |
589 | __set_bit(addr >> PAGE_SHIFT, dma_dom->bitmap); | |
590 | } | |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
431b2a20 JR |
595 | /* |
596 | * Inits the unity mappings required for a specific device | |
597 | */ | |
bd0e5211 JR |
598 | static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom, |
599 | u16 devid) | |
600 | { | |
601 | struct unity_map_entry *e; | |
602 | int ret; | |
603 | ||
604 | list_for_each_entry(e, &amd_iommu_unity_map, list) { | |
605 | if (!(devid >= e->devid_start && devid <= e->devid_end)) | |
606 | continue; | |
607 | ret = dma_ops_unity_map(dma_dom, e); | |
608 | if (ret) | |
609 | return ret; | |
610 | } | |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
431b2a20 JR |
615 | /**************************************************************************** |
616 | * | |
617 | * The next functions belong to the address allocator for the dma_ops | |
618 | * interface functions. They work like the allocators in the other IOMMU | |
619 | * drivers. Its basically a bitmap which marks the allocated pages in | |
620 | * the aperture. Maybe it could be enhanced in the future to a more | |
621 | * efficient allocator. | |
622 | * | |
623 | ****************************************************************************/ | |
d3086444 | 624 | |
431b2a20 JR |
625 | /* |
626 | * The address allocator core function. | |
627 | * | |
628 | * called with domain->lock held | |
629 | */ | |
d3086444 JR |
630 | static unsigned long dma_ops_alloc_addresses(struct device *dev, |
631 | struct dma_ops_domain *dom, | |
6d4f343f | 632 | unsigned int pages, |
832a90c3 JR |
633 | unsigned long align_mask, |
634 | u64 dma_mask) | |
d3086444 | 635 | { |
40becd8d | 636 | unsigned long limit; |
d3086444 | 637 | unsigned long address; |
d3086444 JR |
638 | unsigned long boundary_size; |
639 | ||
640 | boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1, | |
641 | PAGE_SIZE) >> PAGE_SHIFT; | |
40becd8d FT |
642 | limit = iommu_device_max_index(dom->aperture_size >> PAGE_SHIFT, 0, |
643 | dma_mask >> PAGE_SHIFT); | |
d3086444 | 644 | |
1c655773 | 645 | if (dom->next_bit >= limit) { |
d3086444 | 646 | dom->next_bit = 0; |
1c655773 JR |
647 | dom->need_flush = true; |
648 | } | |
d3086444 JR |
649 | |
650 | address = iommu_area_alloc(dom->bitmap, limit, dom->next_bit, pages, | |
6d4f343f | 651 | 0 , boundary_size, align_mask); |
1c655773 | 652 | if (address == -1) { |
d3086444 | 653 | address = iommu_area_alloc(dom->bitmap, limit, 0, pages, |
6d4f343f | 654 | 0, boundary_size, align_mask); |
1c655773 JR |
655 | dom->need_flush = true; |
656 | } | |
d3086444 JR |
657 | |
658 | if (likely(address != -1)) { | |
d3086444 JR |
659 | dom->next_bit = address + pages; |
660 | address <<= PAGE_SHIFT; | |
661 | } else | |
662 | address = bad_dma_address; | |
663 | ||
664 | WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size); | |
665 | ||
666 | return address; | |
667 | } | |
668 | ||
431b2a20 JR |
669 | /* |
670 | * The address free function. | |
671 | * | |
672 | * called with domain->lock held | |
673 | */ | |
d3086444 JR |
674 | static void dma_ops_free_addresses(struct dma_ops_domain *dom, |
675 | unsigned long address, | |
676 | unsigned int pages) | |
677 | { | |
678 | address >>= PAGE_SHIFT; | |
679 | iommu_area_free(dom->bitmap, address, pages); | |
80be308d | 680 | |
8501c45c | 681 | if (address >= dom->next_bit) |
80be308d | 682 | dom->need_flush = true; |
d3086444 JR |
683 | } |
684 | ||
431b2a20 JR |
685 | /**************************************************************************** |
686 | * | |
687 | * The next functions belong to the domain allocation. A domain is | |
688 | * allocated for every IOMMU as the default domain. If device isolation | |
689 | * is enabled, every device get its own domain. The most important thing | |
690 | * about domains is the page table mapping the DMA address space they | |
691 | * contain. | |
692 | * | |
693 | ****************************************************************************/ | |
694 | ||
ec487d1a JR |
695 | static u16 domain_id_alloc(void) |
696 | { | |
697 | unsigned long flags; | |
698 | int id; | |
699 | ||
700 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
701 | id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID); | |
702 | BUG_ON(id == 0); | |
703 | if (id > 0 && id < MAX_DOMAIN_ID) | |
704 | __set_bit(id, amd_iommu_pd_alloc_bitmap); | |
705 | else | |
706 | id = 0; | |
707 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
708 | ||
709 | return id; | |
710 | } | |
711 | ||
a2acfb75 JR |
712 | #ifdef CONFIG_IOMMU_API |
713 | static void domain_id_free(int id) | |
714 | { | |
715 | unsigned long flags; | |
716 | ||
717 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
718 | if (id > 0 && id < MAX_DOMAIN_ID) | |
719 | __clear_bit(id, amd_iommu_pd_alloc_bitmap); | |
720 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
721 | } | |
722 | #endif | |
723 | ||
431b2a20 JR |
724 | /* |
725 | * Used to reserve address ranges in the aperture (e.g. for exclusion | |
726 | * ranges. | |
727 | */ | |
ec487d1a JR |
728 | static void dma_ops_reserve_addresses(struct dma_ops_domain *dom, |
729 | unsigned long start_page, | |
730 | unsigned int pages) | |
731 | { | |
732 | unsigned int last_page = dom->aperture_size >> PAGE_SHIFT; | |
733 | ||
734 | if (start_page + pages > last_page) | |
735 | pages = last_page - start_page; | |
736 | ||
d26dbc5c | 737 | iommu_area_reserve(dom->bitmap, start_page, pages); |
ec487d1a JR |
738 | } |
739 | ||
86db2e5d | 740 | static void free_pagetable(struct protection_domain *domain) |
ec487d1a JR |
741 | { |
742 | int i, j; | |
743 | u64 *p1, *p2, *p3; | |
744 | ||
86db2e5d | 745 | p1 = domain->pt_root; |
ec487d1a JR |
746 | |
747 | if (!p1) | |
748 | return; | |
749 | ||
750 | for (i = 0; i < 512; ++i) { | |
751 | if (!IOMMU_PTE_PRESENT(p1[i])) | |
752 | continue; | |
753 | ||
754 | p2 = IOMMU_PTE_PAGE(p1[i]); | |
3cc3d84b | 755 | for (j = 0; j < 512; ++j) { |
ec487d1a JR |
756 | if (!IOMMU_PTE_PRESENT(p2[j])) |
757 | continue; | |
758 | p3 = IOMMU_PTE_PAGE(p2[j]); | |
759 | free_page((unsigned long)p3); | |
760 | } | |
761 | ||
762 | free_page((unsigned long)p2); | |
763 | } | |
764 | ||
765 | free_page((unsigned long)p1); | |
86db2e5d JR |
766 | |
767 | domain->pt_root = NULL; | |
ec487d1a JR |
768 | } |
769 | ||
431b2a20 JR |
770 | /* |
771 | * Free a domain, only used if something went wrong in the | |
772 | * allocation path and we need to free an already allocated page table | |
773 | */ | |
ec487d1a JR |
774 | static void dma_ops_domain_free(struct dma_ops_domain *dom) |
775 | { | |
776 | if (!dom) | |
777 | return; | |
778 | ||
86db2e5d | 779 | free_pagetable(&dom->domain); |
ec487d1a JR |
780 | |
781 | kfree(dom->pte_pages); | |
782 | ||
783 | kfree(dom->bitmap); | |
784 | ||
785 | kfree(dom); | |
786 | } | |
787 | ||
431b2a20 JR |
788 | /* |
789 | * Allocates a new protection domain usable for the dma_ops functions. | |
790 | * It also intializes the page table and the address allocator data | |
791 | * structures required for the dma_ops interface | |
792 | */ | |
ec487d1a JR |
793 | static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu, |
794 | unsigned order) | |
795 | { | |
796 | struct dma_ops_domain *dma_dom; | |
797 | unsigned i, num_pte_pages; | |
798 | u64 *l2_pde; | |
799 | u64 address; | |
800 | ||
801 | /* | |
802 | * Currently the DMA aperture must be between 32 MB and 1GB in size | |
803 | */ | |
804 | if ((order < 25) || (order > 30)) | |
805 | return NULL; | |
806 | ||
807 | dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL); | |
808 | if (!dma_dom) | |
809 | return NULL; | |
810 | ||
811 | spin_lock_init(&dma_dom->domain.lock); | |
812 | ||
813 | dma_dom->domain.id = domain_id_alloc(); | |
814 | if (dma_dom->domain.id == 0) | |
815 | goto free_dma_dom; | |
816 | dma_dom->domain.mode = PAGE_MODE_3_LEVEL; | |
817 | dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL); | |
9fdb19d6 | 818 | dma_dom->domain.flags = PD_DMA_OPS_MASK; |
ec487d1a JR |
819 | dma_dom->domain.priv = dma_dom; |
820 | if (!dma_dom->domain.pt_root) | |
821 | goto free_dma_dom; | |
822 | dma_dom->aperture_size = (1ULL << order); | |
823 | dma_dom->bitmap = kzalloc(dma_dom->aperture_size / (PAGE_SIZE * 8), | |
824 | GFP_KERNEL); | |
825 | if (!dma_dom->bitmap) | |
826 | goto free_dma_dom; | |
827 | /* | |
828 | * mark the first page as allocated so we never return 0 as | |
829 | * a valid dma-address. So we can use 0 as error value | |
830 | */ | |
831 | dma_dom->bitmap[0] = 1; | |
832 | dma_dom->next_bit = 0; | |
833 | ||
1c655773 | 834 | dma_dom->need_flush = false; |
bd60b735 | 835 | dma_dom->target_dev = 0xffff; |
1c655773 | 836 | |
431b2a20 | 837 | /* Intialize the exclusion range if necessary */ |
ec487d1a JR |
838 | if (iommu->exclusion_start && |
839 | iommu->exclusion_start < dma_dom->aperture_size) { | |
840 | unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT; | |
e3c449f5 JR |
841 | int pages = iommu_num_pages(iommu->exclusion_start, |
842 | iommu->exclusion_length, | |
843 | PAGE_SIZE); | |
ec487d1a JR |
844 | dma_ops_reserve_addresses(dma_dom, startpage, pages); |
845 | } | |
846 | ||
431b2a20 JR |
847 | /* |
848 | * At the last step, build the page tables so we don't need to | |
849 | * allocate page table pages in the dma_ops mapping/unmapping | |
850 | * path. | |
851 | */ | |
ec487d1a JR |
852 | num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512); |
853 | dma_dom->pte_pages = kzalloc(num_pte_pages * sizeof(void *), | |
854 | GFP_KERNEL); | |
855 | if (!dma_dom->pte_pages) | |
856 | goto free_dma_dom; | |
857 | ||
858 | l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL); | |
859 | if (l2_pde == NULL) | |
860 | goto free_dma_dom; | |
861 | ||
862 | dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde)); | |
863 | ||
864 | for (i = 0; i < num_pte_pages; ++i) { | |
865 | dma_dom->pte_pages[i] = (u64 *)get_zeroed_page(GFP_KERNEL); | |
866 | if (!dma_dom->pte_pages[i]) | |
867 | goto free_dma_dom; | |
868 | address = virt_to_phys(dma_dom->pte_pages[i]); | |
869 | l2_pde[i] = IOMMU_L1_PDE(address); | |
870 | } | |
871 | ||
872 | return dma_dom; | |
873 | ||
874 | free_dma_dom: | |
875 | dma_ops_domain_free(dma_dom); | |
876 | ||
877 | return NULL; | |
878 | } | |
879 | ||
5b28df6f JR |
880 | /* |
881 | * little helper function to check whether a given protection domain is a | |
882 | * dma_ops domain | |
883 | */ | |
884 | static bool dma_ops_domain(struct protection_domain *domain) | |
885 | { | |
886 | return domain->flags & PD_DMA_OPS_MASK; | |
887 | } | |
888 | ||
431b2a20 JR |
889 | /* |
890 | * Find out the protection domain structure for a given PCI device. This | |
891 | * will give us the pointer to the page table root for example. | |
892 | */ | |
b20ac0d4 JR |
893 | static struct protection_domain *domain_for_device(u16 devid) |
894 | { | |
895 | struct protection_domain *dom; | |
896 | unsigned long flags; | |
897 | ||
898 | read_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
899 | dom = amd_iommu_pd_table[devid]; | |
900 | read_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
901 | ||
902 | return dom; | |
903 | } | |
904 | ||
431b2a20 JR |
905 | /* |
906 | * If a device is not yet associated with a domain, this function does | |
907 | * assigns it visible for the hardware | |
908 | */ | |
f1179dc0 JR |
909 | static void attach_device(struct amd_iommu *iommu, |
910 | struct protection_domain *domain, | |
911 | u16 devid) | |
b20ac0d4 JR |
912 | { |
913 | unsigned long flags; | |
b20ac0d4 JR |
914 | u64 pte_root = virt_to_phys(domain->pt_root); |
915 | ||
863c74eb JR |
916 | domain->dev_cnt += 1; |
917 | ||
38ddf41b JR |
918 | pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK) |
919 | << DEV_ENTRY_MODE_SHIFT; | |
920 | pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV; | |
b20ac0d4 JR |
921 | |
922 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
38ddf41b JR |
923 | amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root); |
924 | amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root); | |
b20ac0d4 JR |
925 | amd_iommu_dev_table[devid].data[2] = domain->id; |
926 | ||
927 | amd_iommu_pd_table[devid] = domain; | |
928 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
929 | ||
930 | iommu_queue_inv_dev_entry(iommu, devid); | |
b20ac0d4 JR |
931 | } |
932 | ||
355bf553 JR |
933 | /* |
934 | * Removes a device from a protection domain (unlocked) | |
935 | */ | |
936 | static void __detach_device(struct protection_domain *domain, u16 devid) | |
937 | { | |
938 | ||
939 | /* lock domain */ | |
940 | spin_lock(&domain->lock); | |
941 | ||
942 | /* remove domain from the lookup table */ | |
943 | amd_iommu_pd_table[devid] = NULL; | |
944 | ||
945 | /* remove entry from the device table seen by the hardware */ | |
946 | amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV; | |
947 | amd_iommu_dev_table[devid].data[1] = 0; | |
948 | amd_iommu_dev_table[devid].data[2] = 0; | |
949 | ||
950 | /* decrease reference counter */ | |
951 | domain->dev_cnt -= 1; | |
952 | ||
953 | /* ready */ | |
954 | spin_unlock(&domain->lock); | |
955 | } | |
956 | ||
957 | /* | |
958 | * Removes a device from a protection domain (with devtable_lock held) | |
959 | */ | |
960 | static void detach_device(struct protection_domain *domain, u16 devid) | |
961 | { | |
962 | unsigned long flags; | |
963 | ||
964 | /* lock device table */ | |
965 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
966 | __detach_device(domain, devid); | |
967 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
968 | } | |
e275a2a0 JR |
969 | |
970 | static int device_change_notifier(struct notifier_block *nb, | |
971 | unsigned long action, void *data) | |
972 | { | |
973 | struct device *dev = data; | |
974 | struct pci_dev *pdev = to_pci_dev(dev); | |
975 | u16 devid = calc_devid(pdev->bus->number, pdev->devfn); | |
976 | struct protection_domain *domain; | |
977 | struct dma_ops_domain *dma_domain; | |
978 | struct amd_iommu *iommu; | |
1ac4cbbc JR |
979 | int order = amd_iommu_aperture_order; |
980 | unsigned long flags; | |
e275a2a0 JR |
981 | |
982 | if (devid > amd_iommu_last_bdf) | |
983 | goto out; | |
984 | ||
985 | devid = amd_iommu_alias_table[devid]; | |
986 | ||
987 | iommu = amd_iommu_rlookup_table[devid]; | |
988 | if (iommu == NULL) | |
989 | goto out; | |
990 | ||
991 | domain = domain_for_device(devid); | |
992 | ||
993 | if (domain && !dma_ops_domain(domain)) | |
994 | WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound " | |
995 | "to a non-dma-ops domain\n", dev_name(dev)); | |
996 | ||
997 | switch (action) { | |
998 | case BUS_NOTIFY_BOUND_DRIVER: | |
999 | if (domain) | |
1000 | goto out; | |
1001 | dma_domain = find_protection_domain(devid); | |
1002 | if (!dma_domain) | |
1003 | dma_domain = iommu->default_dom; | |
1004 | attach_device(iommu, &dma_domain->domain, devid); | |
1005 | printk(KERN_INFO "AMD IOMMU: Using protection domain %d for " | |
1006 | "device %s\n", dma_domain->domain.id, dev_name(dev)); | |
1007 | break; | |
1008 | case BUS_NOTIFY_UNBIND_DRIVER: | |
1009 | if (!domain) | |
1010 | goto out; | |
1011 | detach_device(domain, devid); | |
1ac4cbbc JR |
1012 | break; |
1013 | case BUS_NOTIFY_ADD_DEVICE: | |
1014 | /* allocate a protection domain if a device is added */ | |
1015 | dma_domain = find_protection_domain(devid); | |
1016 | if (dma_domain) | |
1017 | goto out; | |
1018 | dma_domain = dma_ops_domain_alloc(iommu, order); | |
1019 | if (!dma_domain) | |
1020 | goto out; | |
1021 | dma_domain->target_dev = devid; | |
1022 | ||
1023 | spin_lock_irqsave(&iommu_pd_list_lock, flags); | |
1024 | list_add_tail(&dma_domain->list, &iommu_pd_list); | |
1025 | spin_unlock_irqrestore(&iommu_pd_list_lock, flags); | |
1026 | ||
e275a2a0 JR |
1027 | break; |
1028 | default: | |
1029 | goto out; | |
1030 | } | |
1031 | ||
1032 | iommu_queue_inv_dev_entry(iommu, devid); | |
1033 | iommu_completion_wait(iommu); | |
1034 | ||
1035 | out: | |
1036 | return 0; | |
1037 | } | |
1038 | ||
1039 | struct notifier_block device_nb = { | |
1040 | .notifier_call = device_change_notifier, | |
1041 | }; | |
355bf553 | 1042 | |
431b2a20 JR |
1043 | /***************************************************************************** |
1044 | * | |
1045 | * The next functions belong to the dma_ops mapping/unmapping code. | |
1046 | * | |
1047 | *****************************************************************************/ | |
1048 | ||
dbcc112e JR |
1049 | /* |
1050 | * This function checks if the driver got a valid device from the caller to | |
1051 | * avoid dereferencing invalid pointers. | |
1052 | */ | |
1053 | static bool check_device(struct device *dev) | |
1054 | { | |
1055 | if (!dev || !dev->dma_mask) | |
1056 | return false; | |
1057 | ||
1058 | return true; | |
1059 | } | |
1060 | ||
bd60b735 JR |
1061 | /* |
1062 | * In this function the list of preallocated protection domains is traversed to | |
1063 | * find the domain for a specific device | |
1064 | */ | |
1065 | static struct dma_ops_domain *find_protection_domain(u16 devid) | |
1066 | { | |
1067 | struct dma_ops_domain *entry, *ret = NULL; | |
1068 | unsigned long flags; | |
1069 | ||
1070 | if (list_empty(&iommu_pd_list)) | |
1071 | return NULL; | |
1072 | ||
1073 | spin_lock_irqsave(&iommu_pd_list_lock, flags); | |
1074 | ||
1075 | list_for_each_entry(entry, &iommu_pd_list, list) { | |
1076 | if (entry->target_dev == devid) { | |
1077 | ret = entry; | |
bd60b735 JR |
1078 | break; |
1079 | } | |
1080 | } | |
1081 | ||
1082 | spin_unlock_irqrestore(&iommu_pd_list_lock, flags); | |
1083 | ||
1084 | return ret; | |
1085 | } | |
1086 | ||
431b2a20 JR |
1087 | /* |
1088 | * In the dma_ops path we only have the struct device. This function | |
1089 | * finds the corresponding IOMMU, the protection domain and the | |
1090 | * requestor id for a given device. | |
1091 | * If the device is not yet associated with a domain this is also done | |
1092 | * in this function. | |
1093 | */ | |
b20ac0d4 JR |
1094 | static int get_device_resources(struct device *dev, |
1095 | struct amd_iommu **iommu, | |
1096 | struct protection_domain **domain, | |
1097 | u16 *bdf) | |
1098 | { | |
1099 | struct dma_ops_domain *dma_dom; | |
1100 | struct pci_dev *pcidev; | |
1101 | u16 _bdf; | |
1102 | ||
dbcc112e JR |
1103 | *iommu = NULL; |
1104 | *domain = NULL; | |
1105 | *bdf = 0xffff; | |
1106 | ||
1107 | if (dev->bus != &pci_bus_type) | |
1108 | return 0; | |
b20ac0d4 JR |
1109 | |
1110 | pcidev = to_pci_dev(dev); | |
d591b0a3 | 1111 | _bdf = calc_devid(pcidev->bus->number, pcidev->devfn); |
b20ac0d4 | 1112 | |
431b2a20 | 1113 | /* device not translated by any IOMMU in the system? */ |
dbcc112e | 1114 | if (_bdf > amd_iommu_last_bdf) |
b20ac0d4 | 1115 | return 0; |
b20ac0d4 JR |
1116 | |
1117 | *bdf = amd_iommu_alias_table[_bdf]; | |
1118 | ||
1119 | *iommu = amd_iommu_rlookup_table[*bdf]; | |
1120 | if (*iommu == NULL) | |
1121 | return 0; | |
b20ac0d4 JR |
1122 | *domain = domain_for_device(*bdf); |
1123 | if (*domain == NULL) { | |
bd60b735 JR |
1124 | dma_dom = find_protection_domain(*bdf); |
1125 | if (!dma_dom) | |
1126 | dma_dom = (*iommu)->default_dom; | |
b20ac0d4 | 1127 | *domain = &dma_dom->domain; |
f1179dc0 | 1128 | attach_device(*iommu, *domain, *bdf); |
b20ac0d4 | 1129 | printk(KERN_INFO "AMD IOMMU: Using protection domain %d for " |
ab896722 | 1130 | "device %s\n", (*domain)->id, dev_name(dev)); |
b20ac0d4 JR |
1131 | } |
1132 | ||
f91ba190 | 1133 | if (domain_for_device(_bdf) == NULL) |
f1179dc0 | 1134 | attach_device(*iommu, *domain, _bdf); |
f91ba190 | 1135 | |
b20ac0d4 JR |
1136 | return 1; |
1137 | } | |
1138 | ||
431b2a20 JR |
1139 | /* |
1140 | * This is the generic map function. It maps one 4kb page at paddr to | |
1141 | * the given address in the DMA address space for the domain. | |
1142 | */ | |
cb76c322 JR |
1143 | static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu, |
1144 | struct dma_ops_domain *dom, | |
1145 | unsigned long address, | |
1146 | phys_addr_t paddr, | |
1147 | int direction) | |
1148 | { | |
1149 | u64 *pte, __pte; | |
1150 | ||
1151 | WARN_ON(address > dom->aperture_size); | |
1152 | ||
1153 | paddr &= PAGE_MASK; | |
1154 | ||
1155 | pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)]; | |
1156 | pte += IOMMU_PTE_L0_INDEX(address); | |
1157 | ||
1158 | __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC; | |
1159 | ||
1160 | if (direction == DMA_TO_DEVICE) | |
1161 | __pte |= IOMMU_PTE_IR; | |
1162 | else if (direction == DMA_FROM_DEVICE) | |
1163 | __pte |= IOMMU_PTE_IW; | |
1164 | else if (direction == DMA_BIDIRECTIONAL) | |
1165 | __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW; | |
1166 | ||
1167 | WARN_ON(*pte); | |
1168 | ||
1169 | *pte = __pte; | |
1170 | ||
1171 | return (dma_addr_t)address; | |
1172 | } | |
1173 | ||
431b2a20 JR |
1174 | /* |
1175 | * The generic unmapping function for on page in the DMA address space. | |
1176 | */ | |
cb76c322 JR |
1177 | static void dma_ops_domain_unmap(struct amd_iommu *iommu, |
1178 | struct dma_ops_domain *dom, | |
1179 | unsigned long address) | |
1180 | { | |
1181 | u64 *pte; | |
1182 | ||
1183 | if (address >= dom->aperture_size) | |
1184 | return; | |
1185 | ||
8ad909c4 | 1186 | WARN_ON(address & ~PAGE_MASK || address >= dom->aperture_size); |
cb76c322 JR |
1187 | |
1188 | pte = dom->pte_pages[IOMMU_PTE_L1_INDEX(address)]; | |
1189 | pte += IOMMU_PTE_L0_INDEX(address); | |
1190 | ||
1191 | WARN_ON(!*pte); | |
1192 | ||
1193 | *pte = 0ULL; | |
1194 | } | |
1195 | ||
431b2a20 JR |
1196 | /* |
1197 | * This function contains common code for mapping of a physically | |
24f81160 JR |
1198 | * contiguous memory region into DMA address space. It is used by all |
1199 | * mapping functions provided with this IOMMU driver. | |
431b2a20 JR |
1200 | * Must be called with the domain lock held. |
1201 | */ | |
cb76c322 JR |
1202 | static dma_addr_t __map_single(struct device *dev, |
1203 | struct amd_iommu *iommu, | |
1204 | struct dma_ops_domain *dma_dom, | |
1205 | phys_addr_t paddr, | |
1206 | size_t size, | |
6d4f343f | 1207 | int dir, |
832a90c3 JR |
1208 | bool align, |
1209 | u64 dma_mask) | |
cb76c322 JR |
1210 | { |
1211 | dma_addr_t offset = paddr & ~PAGE_MASK; | |
1212 | dma_addr_t address, start; | |
1213 | unsigned int pages; | |
6d4f343f | 1214 | unsigned long align_mask = 0; |
cb76c322 JR |
1215 | int i; |
1216 | ||
e3c449f5 | 1217 | pages = iommu_num_pages(paddr, size, PAGE_SIZE); |
cb76c322 JR |
1218 | paddr &= PAGE_MASK; |
1219 | ||
6d4f343f JR |
1220 | if (align) |
1221 | align_mask = (1UL << get_order(size)) - 1; | |
1222 | ||
832a90c3 JR |
1223 | address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask, |
1224 | dma_mask); | |
cb76c322 JR |
1225 | if (unlikely(address == bad_dma_address)) |
1226 | goto out; | |
1227 | ||
1228 | start = address; | |
1229 | for (i = 0; i < pages; ++i) { | |
1230 | dma_ops_domain_map(iommu, dma_dom, start, paddr, dir); | |
1231 | paddr += PAGE_SIZE; | |
1232 | start += PAGE_SIZE; | |
1233 | } | |
1234 | address += offset; | |
1235 | ||
afa9fdc2 | 1236 | if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) { |
1c655773 JR |
1237 | iommu_flush_tlb(iommu, dma_dom->domain.id); |
1238 | dma_dom->need_flush = false; | |
1239 | } else if (unlikely(iommu_has_npcache(iommu))) | |
270cab24 JR |
1240 | iommu_flush_pages(iommu, dma_dom->domain.id, address, size); |
1241 | ||
cb76c322 JR |
1242 | out: |
1243 | return address; | |
1244 | } | |
1245 | ||
431b2a20 JR |
1246 | /* |
1247 | * Does the reverse of the __map_single function. Must be called with | |
1248 | * the domain lock held too | |
1249 | */ | |
cb76c322 JR |
1250 | static void __unmap_single(struct amd_iommu *iommu, |
1251 | struct dma_ops_domain *dma_dom, | |
1252 | dma_addr_t dma_addr, | |
1253 | size_t size, | |
1254 | int dir) | |
1255 | { | |
1256 | dma_addr_t i, start; | |
1257 | unsigned int pages; | |
1258 | ||
b8d9905d JR |
1259 | if ((dma_addr == bad_dma_address) || |
1260 | (dma_addr + size > dma_dom->aperture_size)) | |
cb76c322 JR |
1261 | return; |
1262 | ||
e3c449f5 | 1263 | pages = iommu_num_pages(dma_addr, size, PAGE_SIZE); |
cb76c322 JR |
1264 | dma_addr &= PAGE_MASK; |
1265 | start = dma_addr; | |
1266 | ||
1267 | for (i = 0; i < pages; ++i) { | |
1268 | dma_ops_domain_unmap(iommu, dma_dom, start); | |
1269 | start += PAGE_SIZE; | |
1270 | } | |
1271 | ||
1272 | dma_ops_free_addresses(dma_dom, dma_addr, pages); | |
270cab24 | 1273 | |
80be308d | 1274 | if (amd_iommu_unmap_flush || dma_dom->need_flush) { |
1c655773 | 1275 | iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size); |
80be308d JR |
1276 | dma_dom->need_flush = false; |
1277 | } | |
cb76c322 JR |
1278 | } |
1279 | ||
431b2a20 JR |
1280 | /* |
1281 | * The exported map_single function for dma_ops. | |
1282 | */ | |
4da70b9e JR |
1283 | static dma_addr_t map_single(struct device *dev, phys_addr_t paddr, |
1284 | size_t size, int dir) | |
1285 | { | |
1286 | unsigned long flags; | |
1287 | struct amd_iommu *iommu; | |
1288 | struct protection_domain *domain; | |
1289 | u16 devid; | |
1290 | dma_addr_t addr; | |
832a90c3 | 1291 | u64 dma_mask; |
4da70b9e | 1292 | |
0f2a86f2 JR |
1293 | INC_STATS_COUNTER(cnt_map_single); |
1294 | ||
dbcc112e JR |
1295 | if (!check_device(dev)) |
1296 | return bad_dma_address; | |
1297 | ||
832a90c3 | 1298 | dma_mask = *dev->dma_mask; |
4da70b9e JR |
1299 | |
1300 | get_device_resources(dev, &iommu, &domain, &devid); | |
1301 | ||
1302 | if (iommu == NULL || domain == NULL) | |
431b2a20 | 1303 | /* device not handled by any AMD IOMMU */ |
4da70b9e JR |
1304 | return (dma_addr_t)paddr; |
1305 | ||
5b28df6f JR |
1306 | if (!dma_ops_domain(domain)) |
1307 | return bad_dma_address; | |
1308 | ||
4da70b9e | 1309 | spin_lock_irqsave(&domain->lock, flags); |
832a90c3 JR |
1310 | addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false, |
1311 | dma_mask); | |
4da70b9e JR |
1312 | if (addr == bad_dma_address) |
1313 | goto out; | |
1314 | ||
09ee17eb | 1315 | iommu_completion_wait(iommu); |
4da70b9e JR |
1316 | |
1317 | out: | |
1318 | spin_unlock_irqrestore(&domain->lock, flags); | |
1319 | ||
1320 | return addr; | |
1321 | } | |
1322 | ||
431b2a20 JR |
1323 | /* |
1324 | * The exported unmap_single function for dma_ops. | |
1325 | */ | |
4da70b9e JR |
1326 | static void unmap_single(struct device *dev, dma_addr_t dma_addr, |
1327 | size_t size, int dir) | |
1328 | { | |
1329 | unsigned long flags; | |
1330 | struct amd_iommu *iommu; | |
1331 | struct protection_domain *domain; | |
1332 | u16 devid; | |
1333 | ||
146a6917 JR |
1334 | INC_STATS_COUNTER(cnt_unmap_single); |
1335 | ||
dbcc112e JR |
1336 | if (!check_device(dev) || |
1337 | !get_device_resources(dev, &iommu, &domain, &devid)) | |
431b2a20 | 1338 | /* device not handled by any AMD IOMMU */ |
4da70b9e JR |
1339 | return; |
1340 | ||
5b28df6f JR |
1341 | if (!dma_ops_domain(domain)) |
1342 | return; | |
1343 | ||
4da70b9e JR |
1344 | spin_lock_irqsave(&domain->lock, flags); |
1345 | ||
1346 | __unmap_single(iommu, domain->priv, dma_addr, size, dir); | |
1347 | ||
09ee17eb | 1348 | iommu_completion_wait(iommu); |
4da70b9e JR |
1349 | |
1350 | spin_unlock_irqrestore(&domain->lock, flags); | |
1351 | } | |
1352 | ||
431b2a20 JR |
1353 | /* |
1354 | * This is a special map_sg function which is used if we should map a | |
1355 | * device which is not handled by an AMD IOMMU in the system. | |
1356 | */ | |
65b050ad JR |
1357 | static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist, |
1358 | int nelems, int dir) | |
1359 | { | |
1360 | struct scatterlist *s; | |
1361 | int i; | |
1362 | ||
1363 | for_each_sg(sglist, s, nelems, i) { | |
1364 | s->dma_address = (dma_addr_t)sg_phys(s); | |
1365 | s->dma_length = s->length; | |
1366 | } | |
1367 | ||
1368 | return nelems; | |
1369 | } | |
1370 | ||
431b2a20 JR |
1371 | /* |
1372 | * The exported map_sg function for dma_ops (handles scatter-gather | |
1373 | * lists). | |
1374 | */ | |
65b050ad JR |
1375 | static int map_sg(struct device *dev, struct scatterlist *sglist, |
1376 | int nelems, int dir) | |
1377 | { | |
1378 | unsigned long flags; | |
1379 | struct amd_iommu *iommu; | |
1380 | struct protection_domain *domain; | |
1381 | u16 devid; | |
1382 | int i; | |
1383 | struct scatterlist *s; | |
1384 | phys_addr_t paddr; | |
1385 | int mapped_elems = 0; | |
832a90c3 | 1386 | u64 dma_mask; |
65b050ad | 1387 | |
d03f067a JR |
1388 | INC_STATS_COUNTER(cnt_map_sg); |
1389 | ||
dbcc112e JR |
1390 | if (!check_device(dev)) |
1391 | return 0; | |
1392 | ||
832a90c3 | 1393 | dma_mask = *dev->dma_mask; |
65b050ad JR |
1394 | |
1395 | get_device_resources(dev, &iommu, &domain, &devid); | |
1396 | ||
1397 | if (!iommu || !domain) | |
1398 | return map_sg_no_iommu(dev, sglist, nelems, dir); | |
1399 | ||
5b28df6f JR |
1400 | if (!dma_ops_domain(domain)) |
1401 | return 0; | |
1402 | ||
65b050ad JR |
1403 | spin_lock_irqsave(&domain->lock, flags); |
1404 | ||
1405 | for_each_sg(sglist, s, nelems, i) { | |
1406 | paddr = sg_phys(s); | |
1407 | ||
1408 | s->dma_address = __map_single(dev, iommu, domain->priv, | |
832a90c3 JR |
1409 | paddr, s->length, dir, false, |
1410 | dma_mask); | |
65b050ad JR |
1411 | |
1412 | if (s->dma_address) { | |
1413 | s->dma_length = s->length; | |
1414 | mapped_elems++; | |
1415 | } else | |
1416 | goto unmap; | |
65b050ad JR |
1417 | } |
1418 | ||
09ee17eb | 1419 | iommu_completion_wait(iommu); |
65b050ad JR |
1420 | |
1421 | out: | |
1422 | spin_unlock_irqrestore(&domain->lock, flags); | |
1423 | ||
1424 | return mapped_elems; | |
1425 | unmap: | |
1426 | for_each_sg(sglist, s, mapped_elems, i) { | |
1427 | if (s->dma_address) | |
1428 | __unmap_single(iommu, domain->priv, s->dma_address, | |
1429 | s->dma_length, dir); | |
1430 | s->dma_address = s->dma_length = 0; | |
1431 | } | |
1432 | ||
1433 | mapped_elems = 0; | |
1434 | ||
1435 | goto out; | |
1436 | } | |
1437 | ||
431b2a20 JR |
1438 | /* |
1439 | * The exported map_sg function for dma_ops (handles scatter-gather | |
1440 | * lists). | |
1441 | */ | |
65b050ad JR |
1442 | static void unmap_sg(struct device *dev, struct scatterlist *sglist, |
1443 | int nelems, int dir) | |
1444 | { | |
1445 | unsigned long flags; | |
1446 | struct amd_iommu *iommu; | |
1447 | struct protection_domain *domain; | |
1448 | struct scatterlist *s; | |
1449 | u16 devid; | |
1450 | int i; | |
1451 | ||
55877a6b JR |
1452 | INC_STATS_COUNTER(cnt_unmap_sg); |
1453 | ||
dbcc112e JR |
1454 | if (!check_device(dev) || |
1455 | !get_device_resources(dev, &iommu, &domain, &devid)) | |
65b050ad JR |
1456 | return; |
1457 | ||
5b28df6f JR |
1458 | if (!dma_ops_domain(domain)) |
1459 | return; | |
1460 | ||
65b050ad JR |
1461 | spin_lock_irqsave(&domain->lock, flags); |
1462 | ||
1463 | for_each_sg(sglist, s, nelems, i) { | |
1464 | __unmap_single(iommu, domain->priv, s->dma_address, | |
1465 | s->dma_length, dir); | |
65b050ad JR |
1466 | s->dma_address = s->dma_length = 0; |
1467 | } | |
1468 | ||
09ee17eb | 1469 | iommu_completion_wait(iommu); |
65b050ad JR |
1470 | |
1471 | spin_unlock_irqrestore(&domain->lock, flags); | |
1472 | } | |
1473 | ||
431b2a20 JR |
1474 | /* |
1475 | * The exported alloc_coherent function for dma_ops. | |
1476 | */ | |
5d8b53cf JR |
1477 | static void *alloc_coherent(struct device *dev, size_t size, |
1478 | dma_addr_t *dma_addr, gfp_t flag) | |
1479 | { | |
1480 | unsigned long flags; | |
1481 | void *virt_addr; | |
1482 | struct amd_iommu *iommu; | |
1483 | struct protection_domain *domain; | |
1484 | u16 devid; | |
1485 | phys_addr_t paddr; | |
832a90c3 | 1486 | u64 dma_mask = dev->coherent_dma_mask; |
5d8b53cf | 1487 | |
c8f0fb36 JR |
1488 | INC_STATS_COUNTER(cnt_alloc_coherent); |
1489 | ||
dbcc112e JR |
1490 | if (!check_device(dev)) |
1491 | return NULL; | |
5d8b53cf | 1492 | |
13d9fead FT |
1493 | if (!get_device_resources(dev, &iommu, &domain, &devid)) |
1494 | flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32); | |
5d8b53cf | 1495 | |
c97ac535 | 1496 | flag |= __GFP_ZERO; |
5d8b53cf JR |
1497 | virt_addr = (void *)__get_free_pages(flag, get_order(size)); |
1498 | if (!virt_addr) | |
1499 | return 0; | |
1500 | ||
5d8b53cf JR |
1501 | paddr = virt_to_phys(virt_addr); |
1502 | ||
5d8b53cf JR |
1503 | if (!iommu || !domain) { |
1504 | *dma_addr = (dma_addr_t)paddr; | |
1505 | return virt_addr; | |
1506 | } | |
1507 | ||
5b28df6f JR |
1508 | if (!dma_ops_domain(domain)) |
1509 | goto out_free; | |
1510 | ||
832a90c3 JR |
1511 | if (!dma_mask) |
1512 | dma_mask = *dev->dma_mask; | |
1513 | ||
5d8b53cf JR |
1514 | spin_lock_irqsave(&domain->lock, flags); |
1515 | ||
1516 | *dma_addr = __map_single(dev, iommu, domain->priv, paddr, | |
832a90c3 | 1517 | size, DMA_BIDIRECTIONAL, true, dma_mask); |
5d8b53cf | 1518 | |
5b28df6f JR |
1519 | if (*dma_addr == bad_dma_address) |
1520 | goto out_free; | |
5d8b53cf | 1521 | |
09ee17eb | 1522 | iommu_completion_wait(iommu); |
5d8b53cf | 1523 | |
5d8b53cf JR |
1524 | spin_unlock_irqrestore(&domain->lock, flags); |
1525 | ||
1526 | return virt_addr; | |
5b28df6f JR |
1527 | |
1528 | out_free: | |
1529 | ||
1530 | free_pages((unsigned long)virt_addr, get_order(size)); | |
1531 | ||
1532 | return NULL; | |
5d8b53cf JR |
1533 | } |
1534 | ||
431b2a20 JR |
1535 | /* |
1536 | * The exported free_coherent function for dma_ops. | |
431b2a20 | 1537 | */ |
5d8b53cf JR |
1538 | static void free_coherent(struct device *dev, size_t size, |
1539 | void *virt_addr, dma_addr_t dma_addr) | |
1540 | { | |
1541 | unsigned long flags; | |
1542 | struct amd_iommu *iommu; | |
1543 | struct protection_domain *domain; | |
1544 | u16 devid; | |
1545 | ||
5d31ee7e JR |
1546 | INC_STATS_COUNTER(cnt_free_coherent); |
1547 | ||
dbcc112e JR |
1548 | if (!check_device(dev)) |
1549 | return; | |
1550 | ||
5d8b53cf JR |
1551 | get_device_resources(dev, &iommu, &domain, &devid); |
1552 | ||
1553 | if (!iommu || !domain) | |
1554 | goto free_mem; | |
1555 | ||
5b28df6f JR |
1556 | if (!dma_ops_domain(domain)) |
1557 | goto free_mem; | |
1558 | ||
5d8b53cf JR |
1559 | spin_lock_irqsave(&domain->lock, flags); |
1560 | ||
1561 | __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL); | |
5d8b53cf | 1562 | |
09ee17eb | 1563 | iommu_completion_wait(iommu); |
5d8b53cf JR |
1564 | |
1565 | spin_unlock_irqrestore(&domain->lock, flags); | |
1566 | ||
1567 | free_mem: | |
1568 | free_pages((unsigned long)virt_addr, get_order(size)); | |
1569 | } | |
1570 | ||
b39ba6ad JR |
1571 | /* |
1572 | * This function is called by the DMA layer to find out if we can handle a | |
1573 | * particular device. It is part of the dma_ops. | |
1574 | */ | |
1575 | static int amd_iommu_dma_supported(struct device *dev, u64 mask) | |
1576 | { | |
1577 | u16 bdf; | |
1578 | struct pci_dev *pcidev; | |
1579 | ||
1580 | /* No device or no PCI device */ | |
1581 | if (!dev || dev->bus != &pci_bus_type) | |
1582 | return 0; | |
1583 | ||
1584 | pcidev = to_pci_dev(dev); | |
1585 | ||
1586 | bdf = calc_devid(pcidev->bus->number, pcidev->devfn); | |
1587 | ||
1588 | /* Out of our scope? */ | |
1589 | if (bdf > amd_iommu_last_bdf) | |
1590 | return 0; | |
1591 | ||
1592 | return 1; | |
1593 | } | |
1594 | ||
c432f3df | 1595 | /* |
431b2a20 JR |
1596 | * The function for pre-allocating protection domains. |
1597 | * | |
c432f3df JR |
1598 | * If the driver core informs the DMA layer if a driver grabs a device |
1599 | * we don't need to preallocate the protection domains anymore. | |
1600 | * For now we have to. | |
1601 | */ | |
1602 | void prealloc_protection_domains(void) | |
1603 | { | |
1604 | struct pci_dev *dev = NULL; | |
1605 | struct dma_ops_domain *dma_dom; | |
1606 | struct amd_iommu *iommu; | |
1607 | int order = amd_iommu_aperture_order; | |
1608 | u16 devid; | |
1609 | ||
1610 | while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { | |
edcb34da | 1611 | devid = calc_devid(dev->bus->number, dev->devfn); |
3a61ec38 | 1612 | if (devid > amd_iommu_last_bdf) |
c432f3df JR |
1613 | continue; |
1614 | devid = amd_iommu_alias_table[devid]; | |
1615 | if (domain_for_device(devid)) | |
1616 | continue; | |
1617 | iommu = amd_iommu_rlookup_table[devid]; | |
1618 | if (!iommu) | |
1619 | continue; | |
1620 | dma_dom = dma_ops_domain_alloc(iommu, order); | |
1621 | if (!dma_dom) | |
1622 | continue; | |
1623 | init_unity_mappings_for_device(dma_dom, devid); | |
bd60b735 JR |
1624 | dma_dom->target_dev = devid; |
1625 | ||
1626 | list_add_tail(&dma_dom->list, &iommu_pd_list); | |
c432f3df JR |
1627 | } |
1628 | } | |
1629 | ||
6631ee9d JR |
1630 | static struct dma_mapping_ops amd_iommu_dma_ops = { |
1631 | .alloc_coherent = alloc_coherent, | |
1632 | .free_coherent = free_coherent, | |
1633 | .map_single = map_single, | |
1634 | .unmap_single = unmap_single, | |
1635 | .map_sg = map_sg, | |
1636 | .unmap_sg = unmap_sg, | |
b39ba6ad | 1637 | .dma_supported = amd_iommu_dma_supported, |
6631ee9d JR |
1638 | }; |
1639 | ||
431b2a20 JR |
1640 | /* |
1641 | * The function which clues the AMD IOMMU driver into dma_ops. | |
1642 | */ | |
6631ee9d JR |
1643 | int __init amd_iommu_init_dma_ops(void) |
1644 | { | |
1645 | struct amd_iommu *iommu; | |
1646 | int order = amd_iommu_aperture_order; | |
1647 | int ret; | |
1648 | ||
431b2a20 JR |
1649 | /* |
1650 | * first allocate a default protection domain for every IOMMU we | |
1651 | * found in the system. Devices not assigned to any other | |
1652 | * protection domain will be assigned to the default one. | |
1653 | */ | |
6631ee9d JR |
1654 | list_for_each_entry(iommu, &amd_iommu_list, list) { |
1655 | iommu->default_dom = dma_ops_domain_alloc(iommu, order); | |
1656 | if (iommu->default_dom == NULL) | |
1657 | return -ENOMEM; | |
e2dc14a2 | 1658 | iommu->default_dom->domain.flags |= PD_DEFAULT_MASK; |
6631ee9d JR |
1659 | ret = iommu_init_unity_mappings(iommu); |
1660 | if (ret) | |
1661 | goto free_domains; | |
1662 | } | |
1663 | ||
431b2a20 JR |
1664 | /* |
1665 | * If device isolation is enabled, pre-allocate the protection | |
1666 | * domains for each device. | |
1667 | */ | |
6631ee9d JR |
1668 | if (amd_iommu_isolate) |
1669 | prealloc_protection_domains(); | |
1670 | ||
1671 | iommu_detected = 1; | |
1672 | force_iommu = 1; | |
1673 | bad_dma_address = 0; | |
92af4e29 | 1674 | #ifdef CONFIG_GART_IOMMU |
6631ee9d JR |
1675 | gart_iommu_aperture_disabled = 1; |
1676 | gart_iommu_aperture = 0; | |
92af4e29 | 1677 | #endif |
6631ee9d | 1678 | |
431b2a20 | 1679 | /* Make the driver finally visible to the drivers */ |
6631ee9d JR |
1680 | dma_ops = &amd_iommu_dma_ops; |
1681 | ||
26961efe JR |
1682 | #ifdef CONFIG_IOMMU_API |
1683 | register_iommu(&amd_iommu_ops); | |
1684 | #endif | |
1685 | ||
e275a2a0 JR |
1686 | bus_register_notifier(&pci_bus_type, &device_nb); |
1687 | ||
7f26508b JR |
1688 | amd_iommu_stats_init(); |
1689 | ||
6631ee9d JR |
1690 | return 0; |
1691 | ||
1692 | free_domains: | |
1693 | ||
1694 | list_for_each_entry(iommu, &amd_iommu_list, list) { | |
1695 | if (iommu->default_dom) | |
1696 | dma_ops_domain_free(iommu->default_dom); | |
1697 | } | |
1698 | ||
1699 | return ret; | |
1700 | } | |
6d98cd80 JR |
1701 | |
1702 | /***************************************************************************** | |
1703 | * | |
1704 | * The following functions belong to the exported interface of AMD IOMMU | |
1705 | * | |
1706 | * This interface allows access to lower level functions of the IOMMU | |
1707 | * like protection domain handling and assignement of devices to domains | |
1708 | * which is not possible with the dma_ops interface. | |
1709 | * | |
1710 | *****************************************************************************/ | |
1711 | ||
1712 | #ifdef CONFIG_IOMMU_API | |
1713 | ||
1714 | static void cleanup_domain(struct protection_domain *domain) | |
1715 | { | |
1716 | unsigned long flags; | |
1717 | u16 devid; | |
1718 | ||
1719 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); | |
1720 | ||
1721 | for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) | |
1722 | if (amd_iommu_pd_table[devid] == domain) | |
1723 | __detach_device(domain, devid); | |
1724 | ||
1725 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); | |
1726 | } | |
1727 | ||
c156e347 JR |
1728 | static int amd_iommu_domain_init(struct iommu_domain *dom) |
1729 | { | |
1730 | struct protection_domain *domain; | |
1731 | ||
1732 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); | |
1733 | if (!domain) | |
1734 | return -ENOMEM; | |
1735 | ||
1736 | spin_lock_init(&domain->lock); | |
1737 | domain->mode = PAGE_MODE_3_LEVEL; | |
1738 | domain->id = domain_id_alloc(); | |
1739 | if (!domain->id) | |
1740 | goto out_free; | |
1741 | domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL); | |
1742 | if (!domain->pt_root) | |
1743 | goto out_free; | |
1744 | ||
1745 | dom->priv = domain; | |
1746 | ||
1747 | return 0; | |
1748 | ||
1749 | out_free: | |
1750 | kfree(domain); | |
1751 | ||
1752 | return -ENOMEM; | |
1753 | } | |
1754 | ||
98383fc3 JR |
1755 | static void amd_iommu_domain_destroy(struct iommu_domain *dom) |
1756 | { | |
1757 | struct protection_domain *domain = dom->priv; | |
1758 | ||
1759 | if (!domain) | |
1760 | return; | |
1761 | ||
1762 | if (domain->dev_cnt > 0) | |
1763 | cleanup_domain(domain); | |
1764 | ||
1765 | BUG_ON(domain->dev_cnt != 0); | |
1766 | ||
1767 | free_pagetable(domain); | |
1768 | ||
1769 | domain_id_free(domain->id); | |
1770 | ||
1771 | kfree(domain); | |
1772 | ||
1773 | dom->priv = NULL; | |
1774 | } | |
1775 | ||
684f2888 JR |
1776 | static void amd_iommu_detach_device(struct iommu_domain *dom, |
1777 | struct device *dev) | |
1778 | { | |
1779 | struct protection_domain *domain = dom->priv; | |
1780 | struct amd_iommu *iommu; | |
1781 | struct pci_dev *pdev; | |
1782 | u16 devid; | |
1783 | ||
1784 | if (dev->bus != &pci_bus_type) | |
1785 | return; | |
1786 | ||
1787 | pdev = to_pci_dev(dev); | |
1788 | ||
1789 | devid = calc_devid(pdev->bus->number, pdev->devfn); | |
1790 | ||
1791 | if (devid > 0) | |
1792 | detach_device(domain, devid); | |
1793 | ||
1794 | iommu = amd_iommu_rlookup_table[devid]; | |
1795 | if (!iommu) | |
1796 | return; | |
1797 | ||
1798 | iommu_queue_inv_dev_entry(iommu, devid); | |
1799 | iommu_completion_wait(iommu); | |
1800 | } | |
1801 | ||
01106066 JR |
1802 | static int amd_iommu_attach_device(struct iommu_domain *dom, |
1803 | struct device *dev) | |
1804 | { | |
1805 | struct protection_domain *domain = dom->priv; | |
1806 | struct protection_domain *old_domain; | |
1807 | struct amd_iommu *iommu; | |
1808 | struct pci_dev *pdev; | |
1809 | u16 devid; | |
1810 | ||
1811 | if (dev->bus != &pci_bus_type) | |
1812 | return -EINVAL; | |
1813 | ||
1814 | pdev = to_pci_dev(dev); | |
1815 | ||
1816 | devid = calc_devid(pdev->bus->number, pdev->devfn); | |
1817 | ||
1818 | if (devid >= amd_iommu_last_bdf || | |
1819 | devid != amd_iommu_alias_table[devid]) | |
1820 | return -EINVAL; | |
1821 | ||
1822 | iommu = amd_iommu_rlookup_table[devid]; | |
1823 | if (!iommu) | |
1824 | return -EINVAL; | |
1825 | ||
1826 | old_domain = domain_for_device(devid); | |
1827 | if (old_domain) | |
1828 | return -EBUSY; | |
1829 | ||
1830 | attach_device(iommu, domain, devid); | |
1831 | ||
1832 | iommu_completion_wait(iommu); | |
1833 | ||
1834 | return 0; | |
1835 | } | |
1836 | ||
c6229ca6 JR |
1837 | static int amd_iommu_map_range(struct iommu_domain *dom, |
1838 | unsigned long iova, phys_addr_t paddr, | |
1839 | size_t size, int iommu_prot) | |
1840 | { | |
1841 | struct protection_domain *domain = dom->priv; | |
1842 | unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE); | |
1843 | int prot = 0; | |
1844 | int ret; | |
1845 | ||
1846 | if (iommu_prot & IOMMU_READ) | |
1847 | prot |= IOMMU_PROT_IR; | |
1848 | if (iommu_prot & IOMMU_WRITE) | |
1849 | prot |= IOMMU_PROT_IW; | |
1850 | ||
1851 | iova &= PAGE_MASK; | |
1852 | paddr &= PAGE_MASK; | |
1853 | ||
1854 | for (i = 0; i < npages; ++i) { | |
1855 | ret = iommu_map_page(domain, iova, paddr, prot); | |
1856 | if (ret) | |
1857 | return ret; | |
1858 | ||
1859 | iova += PAGE_SIZE; | |
1860 | paddr += PAGE_SIZE; | |
1861 | } | |
1862 | ||
1863 | return 0; | |
1864 | } | |
1865 | ||
eb74ff6c JR |
1866 | static void amd_iommu_unmap_range(struct iommu_domain *dom, |
1867 | unsigned long iova, size_t size) | |
1868 | { | |
1869 | ||
1870 | struct protection_domain *domain = dom->priv; | |
1871 | unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE); | |
1872 | ||
1873 | iova &= PAGE_MASK; | |
1874 | ||
1875 | for (i = 0; i < npages; ++i) { | |
1876 | iommu_unmap_page(domain, iova); | |
1877 | iova += PAGE_SIZE; | |
1878 | } | |
1879 | ||
1880 | iommu_flush_domain(domain->id); | |
1881 | } | |
1882 | ||
645c4c8d JR |
1883 | static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom, |
1884 | unsigned long iova) | |
1885 | { | |
1886 | struct protection_domain *domain = dom->priv; | |
1887 | unsigned long offset = iova & ~PAGE_MASK; | |
1888 | phys_addr_t paddr; | |
1889 | u64 *pte; | |
1890 | ||
1891 | pte = &domain->pt_root[IOMMU_PTE_L2_INDEX(iova)]; | |
1892 | ||
1893 | if (!IOMMU_PTE_PRESENT(*pte)) | |
1894 | return 0; | |
1895 | ||
1896 | pte = IOMMU_PTE_PAGE(*pte); | |
1897 | pte = &pte[IOMMU_PTE_L1_INDEX(iova)]; | |
1898 | ||
1899 | if (!IOMMU_PTE_PRESENT(*pte)) | |
1900 | return 0; | |
1901 | ||
1902 | pte = IOMMU_PTE_PAGE(*pte); | |
1903 | pte = &pte[IOMMU_PTE_L0_INDEX(iova)]; | |
1904 | ||
1905 | if (!IOMMU_PTE_PRESENT(*pte)) | |
1906 | return 0; | |
1907 | ||
1908 | paddr = *pte & IOMMU_PAGE_MASK; | |
1909 | paddr |= offset; | |
1910 | ||
1911 | return paddr; | |
1912 | } | |
1913 | ||
26961efe JR |
1914 | static struct iommu_ops amd_iommu_ops = { |
1915 | .domain_init = amd_iommu_domain_init, | |
1916 | .domain_destroy = amd_iommu_domain_destroy, | |
1917 | .attach_dev = amd_iommu_attach_device, | |
1918 | .detach_dev = amd_iommu_detach_device, | |
1919 | .map = amd_iommu_map_range, | |
1920 | .unmap = amd_iommu_unmap_range, | |
1921 | .iova_to_phys = amd_iommu_iova_to_phys, | |
1922 | }; | |
1923 | ||
6d98cd80 | 1924 | #endif |